Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(
        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.

array(
            '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
1  
this answer may help you. –  Milo Jun 8 '12 at 13:34

1 Answer 1

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

 
discard

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.