1

i'm trying to get_users with the meta_query where i need to find users where either they don't have the meta key, or it is blank.

It seems like everywhere i look, i should be able to use the OR on the relation and then use the same meta key, but how ever i set it up, it does not work.

My code:

$ors[] = array(
   'key'   => 'death_date',
   'compare' => '=',
   'value' => '',
   'type' => 'NUMERIC'                    
);
$ors[] = array(
   'key'   => 'death_date',
   'compare' => 'NOT EXISTS'                    
);

$args = array(
   'meta_query' => array(
   'relation' => 'or',
   $ors,
));

$return = get_users($args);

Both of them work separately, but not together.

  • note that NOT EXISTS style queries are very slow/expensive on the database. If you can, always give users a death date, even if it's a special value such as the 1st January 30,000AD, and do it via a filter. Then you c an avoid these kinds of queries entirely – Tom J Nowell Dec 6 '19 at 16:11

Your Answer

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

Browse other questions tagged or ask your own question.