Query based on whether posts have a featured image

Ever need to query for posts based whether they have a post thumbnail? This can be accomplished by adding 'meta_key' => '_thumbnail_id' to the query args:

1
2
3
4
5
6
7
8
9
<?php
$args = array(
    'posts_per_page' => 1,
    'meta_key' => '_thumbnail_id'
);
$my_query = new WP_Query( $args );
 
if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post();
...