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 former Core Developer for the WordPress open source project, and he works on cool plugins like AffiliateWP, Easy Digital Downloads, and Restrict Content Pro.

Leave a Reply

Your email address will not be published. Required fields are marked *