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

How can I build a query to find posts that DO NOT contain a certain meta key or meta value?

for example:

query_posts( array( 
    'meta_query' => array(
        array( 'key' => 'feature', 'value' => '_wp_zero_value', 'compare' => '!=' )         )
) );
share|improve this question
up vote 7 down vote accepted

Getting posts without a certain meta key is a little tricky, namely due to the database design and the nature of SQL joins.

AFAIK, the most efficient way would be to actually grab the post IDs that do have the meta key, and then exclude them from your query.

// get all post IDs that *have* 'meta_key' with a non-empty value
$posts = $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'my_key' AND meta_value != ''" );

// get all posts *excluding* the ones we just found
query_posts( array( 'post__not_in' => $posts ) );
share|improve this answer

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.