Display only posts with thumbnail using WP_Query

You need to define your arguments before you pass them to WP_Query, not after. Also, your meta_query should be an array of an array, not just an array

This

 $query = new WP_Query($thumbs);
 $thumbs = array(
        'meta_query' => array('key' => '_thumbnail_id') 
 );

should look like this

 $thumbs = array(
    'meta_query' => array( 
        array(
            'key' => '_thumbnail_id'
        ) 
    )
 );

 $query = new WP_Query($thumbs);

EDIT

Just a few extra notes

  • Make sure to reset postdata after a custom query. Just add wp_reset_postdata(); before you close your if statement and just after closing your while statement

  • I believe that a custom query might not be necessary here. If I read your question correctly, you can simply use pre_get_posts to alter the main query. You shouldn’t use a custom query just because you want to alter the main query

Leave a Comment

tech