I am trying to do a get_users() when the meta key has two values. For instance, in the db it looks like this :


When I do my query :

$claimed_users = get_users('meta_key=username');

and try to output it : print_r($claimed_users);

it just returns Array().

What am I doing wrong? Is there a better way to do this?

  • The meta_key has two values? That's... strange. Do you mean the meta_value is has two values? – Jacob Peattie Aug 31 '17 at 11:27
  • try var_dump($claimed_users); – David Lee Aug 31 '17 at 12:48

I'm assuming that the meta_value has a serialized array, not the meta_key (For it would make no sense to have the meta_key to be a serialized array)

You really should save the values in seperate fields to be able to query it by wordpress internal functions like get_users.

If you definitely have to preserve this data-structure, you have to go by directly accessing the Database by wpdb like this:

global $wpdb;
$claimed_user_ids = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value LIKE %s",'<META_KEY>','%username%'));

(replace with the meta_key which holds the serialized array).

Happy Coding, Kuchenundkakao

|improve this answer|||||

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.