0

I am trying to search for posts that do not have yet set a particular custom field. The problem is that query down below returns an empty result.

$args = array(
    'post_type'  => 'post',
    'posts_per_page' => 5,
    'meta_query' => array(
        array(
            'key'     => 'verified',
            'value'   => 'yes',
            'compare' => 'NOT LIKE'
        )
    )
);
$query = new WP_Query( $args );
1
  • 2
    If the key doesn't exist, you want NOT EXISTS
    – Milo
    Commented Jan 5, 2015 at 18:02

1 Answer 1

3

Try using 'NOT EXISTS' instead of 'NOT LIKE' and remove the value ( it'll be removed by WP with NOT EXISTS ):

$args = array(
    'post_type'  => 'post',
    'posts_per_page' => 5,
    'meta_query' => array(
        array(
            'key'     => 'verified',
            'compare' => 'NOT EXISTS'
        )
    )
);
$query = new WP_Query( $args );

http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

1
  • Ah, Milo beats me by 35 seconds!
    – skim-
    Commented Jan 5, 2015 at 18:03

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.