WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to filter posts by featured image when initialising a WP_Query object?


$my_query = WP_Query(array("has_thumbnail"=>true));

or, more ideally

$my_query = WP_Query(array("has_thumbnail_size"=>"custom_size"));
share|improve this question

Technically featured image is custom field with name _thumbnail_id that holds attachment ID. So you can easily query with something like:

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

Size on other hand is attribute of that attachment and not post itself. You will need to loop through attachment and get sizes they have (if I remember right it should be in data returned by wp_get_attachment_metadata()).

share|improve this answer

Here's an alternate approach of filtering the posts after the query using whether the wp_get_attachment_thumb_url() returns an empty string to determine if the post has a thumbnail (featured image).

//get all the posts however your query does it. simple example when there aren't a lot: 
$args = array("nopaging" => true);
$posts = (new WP_Query($args))->posts;

//filter the posts array by looking up the thumb_url and seeing if it's empty
$posts_without_thumbnails = array_filter($posts,  function($post){
  $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
  return ( wp_get_attachment_thumb_url( $post_thumbnail_id ) == "");
} );

If you need this to happen during the query, you can probably make a wordpress filter or action out of it or intercept the query_posts.

share|improve this answer

Post thumbnails in WP are stored as meta-data. In /wp-includes/post-thumbnail-template.php, WP retrieves the post thumbnail ID by using the _thumbnail_id value, which can be used as a filter in your WP_Query. When used in this way, WP will check the retrieved posts to see if that meta_key value exists, and if so, it will include those posts in the query, excluding all others. Using the example provided, it would look something like this.

$my_query = WP_Query(array('meta_key'=>'_thumbnail_id'));
share|improve this answer
Please explain why this will solve the problem, how it should be used and does it work. – Pieter Goosen Jul 21 '14 at 19:14
Answer updated. Please let me know if further explanation is needed. Sorry, there's not many details out there on this, so you kinda have to dig for it. – nnoom Jul 22 '14 at 0:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.