How to query users by meta value, even when meta key doesn’t exist?

The problem is that users for whom votes_total doesn’t exist are getting skipped. See below:

   $args = array(
       'meta_query' => array(
          'relation' => 'OR',
          array(
             'key' => 'votes_total',
             'compare' => '>',
             'value' => 0,
           ),
           array(
             'key' => 'votes_total',
             'compare' => 'NOT EXISTS',
             'value' => 0,
          ),
      ),
      'meta_key' => 'votes_total',
      'orderby' => 'meta_value_num',
      'order' => 'DESC'
   );
   $users = get_users($args);

1 Answer
1

Maybe this will answer your question:

(Note: Due to bug #23268, value was required for NOT EXISTS comparisons to work correctly prior to 3.9. You had to supply some string for the value parameter. An empty string or NULL will NOT work. However, any other string will do the trick and will NOT show up in your SQL when using NOT EXISTS. Need inspiration? How about ‘bug #23268’.)

On https://codex.wordpress.org/Class_Reference/WP_Meta_Query

Leave a Comment