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:

<?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();
...

Published by

Drew Jaynes

Drew is a Core Developer for the WordPress open source project, and works on cool plugins like AffiliateWP, Easy Digital Downloads, and Restrict Content Pro.

Leave a Reply