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:

<br />
&lt;?php<br />
$args = array(<br />
	'posts_per_page' =&gt; 1,<br />
	'meta_key' =&gt; '_thumbnail_id'<br />
);<br />
$my_query = new WP_Query( $args );</p>
<p>if ( $my_query-&gt;have_posts() ) : while ( $my_query-&gt;have_posts() ) : $my_query-&gt;the_post();<br />
...<br />

Published by

Drew Jaynes

Drew is a formerCore 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