How to search usermeta table

时间:2020-05-20 作者:Johnnyboy Gomez

搜索usermeta以基于meta\\u值查找user\\u id的最佳方法是什么?例如,在usermeta表中,有一行user\\u id=123,meta\\u key=deviceid,meta\\u value=45545。我知道设备id,我需要找出与之关联的user\\u id。我试过(在其他一百万件事情中)这样:

$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 ;

1 个回复
SO网友:Johnnyboy Gomez

这项工作:

$WhoIsUser = get_users(
  array(
   \'meta_key\' => \'deviceid\',
   \'meta_value\' => \'45545\'
 )
);
这将从users表中返回该用户的整行。如果这样打印出来:

echo \'<pre>\'
echo print_r($WhoIsUser, TRUE);
echo \'</pre>\'
您可以获得:

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....
要仅访问ID,我写了以下内容:

$WhoIsUser[0]->ID;