1

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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