What is the best way to search usermeta to find a user_id based on a meta_value? For example in usermeta table there is a row with user_id = 123 , meta_key = deviceid and meta_value = 45545. I know the deviceid and I need to find out what user_id is associated with it. I tried (amongst a million other things) this:

$scaleData = json_decode($reading, TRUE);
$deviceid = $scaleData["imei"];
$WhoIsUser = $wpdb->get_var(  $wpdb->prepare("SELECT user_id FROM wp_6cwmmr_usermeta WHERE meta_value = '$deviceid' "));
echo $WhoIsUser ;
  • Check WP_User_Query, or get_users, but generally meta tables aren't designed for searching and filtering, that's what taxonomies are for. User can have taxonomies too, but there's no UI for displaying them in WP Admin, luckily adding in the term UI is not that difficult, and you'd need to be adding a custom control for the user edit screen anyway. I forget the specifics or I'd write an answer, but Justin Tadlock wrote something a while ago that walked through user taxonomies. The performance difference between the two is night and day
    – Tom J Nowell
    May 20, 2020 at 18:40

1 Answer 1


This works:

$WhoIsUser = get_users(
   'meta_key' => 'deviceid',
   'meta_value' => '45545'

This returns the whole row for that user from the users table. If you print_r it out like so:

echo '<pre>'
echo print_r($WhoIsUser, TRUE);
echo '</pre>'

you get:

    [0] => WP_User Object
            [data] => stdClass Object
                    [ID] => 123
                    [user_login] => userman
                    [user_pass] => $P$mBH8dpvhJD5xN8pk.
                    [user_nicename] => usermans
                    [user_email] => [email protected]
                    [user_url] => 
                    [user_registered] => 2020-05-11 14:36:24
                    [user_activation_key] => 157784:Fj8SR051xbzUVMpAFcYI0
                    [user_status] => 0
                    [display_name] => J B Gomez

            [ID] => 123
            [caps] => Array
                    [subscriber] => 1
and so on....

To access just the ID I wrote this:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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