Skip posts without a thumbnail in loop

You can try to add

                'meta_key'     => '_thumbnail_id',

to your input arguments:

$args = array(  'numberposts'  => 5,  
                'orderby'      => 'date',  
                'order'        => 'DESC',
                'post_type'    => 'post',
                'post_status'  => 'publish',
                'meta_key'     => '_thumbnail_id',
            );

to query only posts with thumbnails (i.e. featured images).

ps: instead of this structure:

if ( !has_post_thumbnail() ) { 
        continue;             
} else {

}

you can in general use

if ( has_post_thumbnail() ) { 

}

But you can now skip the if-sentence part in the loop since you are now only fetching posts with featured images.

Leave a Comment