最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mysql - How to search usermeta table

programmeradmin0浏览0评论

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 ;

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 ;
Share Improve this question edited May 20, 2020 at 19:30 Johnnyboy Gomez asked May 20, 2020 at 18:11 Johnnyboy GomezJohnnyboy Gomez 333 bronze badges 1
  • 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 Commented May 20, 2020 at 18:40
Add a comment  | 

1 Answer 1

Reset to default 0

This works:

$WhoIsUser = get_users(
  array(
   '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:

Array
(
    [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:

$WhoIsUser[0]->ID;

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论