Using OR in WP_Query negates the “NOT EXISTS” compare

I’m attempting to filter posts by a custom field that either are equal to ‘new’ or have not been set yet. I’m able to get either of those meta_queries to work on their own but when I use them in an ‘OR’ query, the ‘NOT EXISTS” portion is ignored and the query returns every post that has ANY value set. Here is the sample ‘meta_query’ that is causing the strange behavior:

array(
  'relation' => 'OR',
  array(
    'key' => 'status',
    'compare' => 'NOT EXISTS',
  ),
  array(
    'key' => 'status',
    'value' => 'new',
    'compare' => '=',
  ),
);

Related posts

Leave a Reply

1 comment

  1. array(
      'relation' => 'OR',
      array(
        'key' => 'status',
        'value' => '', //<--- not required but necessary in this case
        'compare' => 'NOT EXISTS',
      ),
      array(
        'key' => 'status',
        'value' => 'new',
        'compare' => '=',
      ),
    );
    

    That should do it.