post_count for only posts with thumbnail

how to edit this condition in order to count only the posts with thumbnails?

if ( $my_query->have_posts() && ( ( $my_query->post_count ) >= 3 ) )

Related posts

Leave a Reply

3 comments

  1. You have to check it in a different way;

    $post_with_thumbs = 0;
    if ( $my_query->have_posts() ) {
      while ( $my_query->have_posts() ): $my_query->the_post();
        if (  has_post_thumbnail()) {
                $post_with_thumbs++;
            }
      endwhile;
    }
    wp_reset_query();
    
    if ($post_with_thumbs >= 3) {
        //do stuff
    }
    
  2. You must do the loop, because thumbnail relationships are not resolved automatically:

    $postsWithThumbs = 0;
    
    while($my_query->have_posts()){
      $my_query->the_post();
    
      if(has_post_thumbnail())
        $postsWithThumbs++;
    }
    
    wp_reset_postdata();
    
    print $postsWithThumbs;