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

The following code fetches posts with the meta key home telephone numbers.

$metas = ''; 
    $args = array(
    'numberposts' => 1,
    'post_type' => 'electors',
    'orderby' => 'post_date',
    'order' => 'ASC',
    'meta_query' => array(
            'key' => 'wpcf-home_telephone_number',
            'value' => '0',
            'compare' => '>'

$canvassing = get_posts( $args );

I want to add another meta to look for. The problem is that this meta may not exist on all posts. I want to get posts that either don't have the meta key or don't have a value for it.

            'key' => 'wpcf-success',

            'value' => ''

This code will only get posts that have the key without a value. I also want posts that do not have this key at all.

I tired this add_post_meta($post->ID, 'wpcf-success', '', true); but since we have not fetched the posts there is no post ID yet.

share|improve this question
this answer may help you. – Milo Jun 8 '12 at 13:34

This "not exists" functionality isn't possible using a regular meta query. It's being worked on, but it looks like it's been shuffled to a future release (see this trac ticket for more insight).

In the meantime, you have to run a custom SQL statement using $wpdb, as in DeadMedic's answer that Milo linked to in the comments.

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.