1

want to query 100 posts, but only those which do not have a featured image attached. I am using meta_key method for this with WP_Query as such:

$args = array(
   'post_type'  => 'post',
   'posts_per_page' => 100,
    'meta_query' => array(
        array(
         'key' => '_thumbnail_id',
         'compare' => 'NOT EXISTS'
        ),
    )
);

But Not work with me

Any help ?

3
  • Please explain But Not work with me Jul 20, 2016 at 9:08
  • my code its not show me any result Jul 20, 2016 at 10:51
  • use if statement like this if(!has_post_thumbnail())
    – user93819
    Jul 21, 2016 at 2:33

2 Answers 2

4

Try this code , its working for me

value=>'?' 

because(value was required for NOT EXISTS comparisons to work correctly prior to 3.9. You had to supply some string for the value parameter. An empty string or NULL will NOT work. However, any other string will do the trick and will NOT show up in your SQL when using NOT EXISTS.)

For more information about value=>? please refer this link

$args = array(
  'post_type'  => 'post',
  'posts_per_page' => 100,
  'meta_query' => array(
     array(
       'key' => '_thumbnail_id',
       'value' => '?',
       'compare' => 'NOT EXISTS'
     )
  ),
);
$new_query = new WP_Query( $args );
0
0

Adding 'value' => '?' doesn't make sense in this context:

In this code, even if 'value' is set, it's actually ignored by WordPress when using 'compare' => 'NOT EXISTS'. In other words, WordPress doesn't take into account the value provided for the 'value' key when it checks for the existence or non-existence of a meta key.

In conclusion, while this code might work in practice (since 'value' is ignored), my example is more precise and clear. It's better to use my example to avoid any potential confusion.

$args = array(
    'post_type'     => 'post',
    'posts_per_page'=> -1,
    'post_status'   => array('publish', 'draft', 'pending'),
    'meta_query'    => array(
       array(
         'key' => '_thumbnail_id',
         'compare' => 'NOT EXISTS'
       )
    ),
);
$query = new WP_Query( $args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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