Title: get_users multiple meta keys
Last modified: September 18, 2020

---

# get_users multiple meta keys

 *  Resolved [candell](https://wordpress.org/support/users/candell/)
 * (@candell)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/get_users-multiple-meta-keys/)
 * Hi
 * I am displaying a list of users that have a value for a meta field ‘offer’;
 *     ```
       $usersWithOffer = get_users(array(
                       'meta_key' => 'offer',
                       'meta_compare'  =>  '!==',
                       'meta_value' => ''
                   ));
       ```
   
 * I also need to order this list by another meta field ‘business’.
 * I added the other key and tried to order by it but it isn;t working
 *     ```
       $usersWithOffer = get_users(array(
                       'meta_key' => 'offer',
                       'meta_compare'  =>  '!==',
                       'meta_value' => '',
                       'meta_key' => 'business',
                   'orderby' => 'business',
                   'order' => 'ASC'
                   ));
       ```
   
 * Can anyone let me know where I have gone wrong?
 * Thanks

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Prashant Singh](https://wordpress.org/support/users/prashantvatsh/)
 * (@prashantvatsh)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/get_users-multiple-meta-keys/#post-13429105)
 * Please try the following code:
 *     ```
       $args = array(
           'meta_query' => array(
               array(
                   'key' => 'offer',
                   'value' => '',
                   'compare' => '!=='
               ),
               array(
                   'key' => 'business',
                   'value' => '',
                   'compare' => '!=='
               )
           )
       );
       $usersWithOffer = get_users($args);
       ```
   
 * I hope it will solve your problem.
    -  This reply was modified 5 years, 8 months ago by [Prashant Singh](https://wordpress.org/support/users/prashantvatsh/).
 *  Thread Starter [candell](https://wordpress.org/support/users/candell/)
 * (@candell)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/get_users-multiple-meta-keys/#post-13429237)
 * Hi
 * Thanks for the help, unfortunately it isn’t ordering by business still.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/get_users-multiple-meta-keys/#post-13435682)
 * According to docs, `'!=='` is not a valid compare value, only `'!='`. I’ve not
   investigated further. In any case, you can use `'NOT EXISTS'` instead. Then AFAIK
   no value argument is needed. If you really need to check type not match with `!
   ==`, use the ‘type’ argument to cast the value to whatever it’s supposed to be.
 * To order by a meta value, the main args array needs a “meta_key” arg outside 
   of the “meta_query” arg arrays. Don’t provide an accompanying “meta_value” arg
   or it’ll conflict with the “meta_query”. I think you only want ordering by business,
   not to qualify the results by it. The only qualification would be for “offer”
   not existing, correct?
 * If so I think this is what you need:
 *     ```
       $args = array(
           'meta_key' => 'business',
           'meta_query' => array(
               array(
                   'key' => 'offer',
                   'compare' => 'NOT EXISTS',
               ),
           ),
           'order_by' => 'meta_value',
       );
       $usersWithOffer = get_users($args);
       ```
   
 * If you still have trouble, we should examine the resulting SQL to determine the
   problem. We may need to filter the actual SQL to correct any portion that is 
   not as desired.
 *  Thread Starter [candell](https://wordpress.org/support/users/candell/)
 * (@candell)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/get_users-multiple-meta-keys/#post-13437931)
 * Thank you for the reply, I want to show users who **do** have a value set under
   offer.
 * I changed your ‘compare’ => ‘NOT EXISTS’, to ‘compare’ => ‘EXISTS’,
 * Here’s the generated query, it seems to be ignoring order by
 *     ```
       SELECT wpsc_users.*
       FROM wpsc_users
       INNER JOIN wpsc_usermeta
       ON ( wpsc_users.ID = wpsc_usermeta.user_id )
       INNER JOIN wpsc_usermeta AS mt1
       ON ( wpsc_users.ID = mt1.user_id )
       WHERE 1=1
       AND ( wpsc_usermeta.meta_key = 'business'
       AND ( mt1.meta_key = 'offer' ) )
       ORDER BY user_login ASC
       ```
   
 * whereas it should be ordering by ‘business’ which is a meta field.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/get_users-multiple-meta-keys/#post-13439587)
 * Argh, my bad. Please use “orderby”, not “order_by”.
 *  Thread Starter [candell](https://wordpress.org/support/users/candell/)
 * (@candell)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/get_users-multiple-meta-keys/#post-13442476)
 * OMG, I didn’t notice either. Thanks, that is working 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘get_users multiple meta keys’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 3 participants
 * Last reply from: [candell](https://wordpress.org/support/users/candell/)
 * Last activity: [5 years, 8 months ago](https://wordpress.org/support/topic/get_users-multiple-meta-keys/#post-13442476)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
