compare meta_query in get_posts arguments

If we define the conditions:

A: my_custom_field_ignore EXISTS
B: my_custom_field_ignore = 1

then NOT ( A && B ) is equivalent to:

NOT ( A ) || NOT ( B )

meaning in our case:

( my_custom_field_ignore NOT EXISTS ) ||  ( my_custom_field_ignore != 1 ) 

We could therefore try the following for WP 3.5+ (untested):

 $args = array(
    'post_type'  => $post_type,
    'offset'     => $offset,
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'my_custom_field_ignore',
            'value'   => '1',
            'compare' => '!=',
        ),
        array(
            'key'     => 'my_custom_field_ignore',
            'compare' => 'NOT EXISTS',
            'value'   => '1',     #<-- just some value as a pre 3.9 bugfix (Codex)
        ),
    )
);

Leave a Comment