Wordpress - 从自定义用户元获取userID

时间:2013-09-09 18:55:44

标签: php wordpress userid meta-key

我尝试从注册期间创建的userMeta密钥中获取wordpress USER ID

用户元键是'lh_scuritycode',元值为$_GET["key"]

$_GET["key"] = f16dea7e995b14e420ddb96fb7dbd67f

这是我的代码:

<?php

if ((ctype_alnum($_GET["key"])) && (strlen($_GET["key"]) == 32)) {

    $lh_users = get_users( array(
                    "meta_key" => "lh_scuritycode",
                    "meta_value" => $_GET["key"],
                    "number" => 1,
                    "count_total" => false
                ) );

    foreach ($lh_users as $lh_user) {
        echo '<li>' . $lh_user->id . '</li>';
    }

} else {    
    echo "Der String ".$_GET["key"]." ist fehlerhaft.\n";   
}

?>

现在,$lh_users只是我echo的数组。当我使用foreach它有效。如果没有foreach,我怎么能这样做?

当我这样做“var_dump”时,结果是:

  

array(1){[0] =&gt; object(WP_User)#126(7){[“data”] =&gt;   object(stdClass)#2077(10){[“ID”] =&gt; string(2)“24”[“user_login”] =&gt;   string(7)“Aner”[“user_pass”] =&gt; string(34)“$ P $ BDpMKv0”   [ “user_nicename”] =&GT; string(7)“aner”[“user_email”] =&gt;串(18)   “infe”[“user_url”] =&gt; string(0)“”[“user_registered”] =&gt;   string(19)“2013-0 1:02”[“user_activation_key”] =&gt;串(0)   “”[“user_status”] =&gt; string(1)“0”[“display_name”] =&gt;串(7)   “Aner”} [“ID”] =&gt; int(24)[“caps”] =&gt; array(1){[“subscriber”] =&gt;   bool(true)} [“cap_key”] =&gt; string(22)“localhero_capabilities”   [ “角色”] =&GT; array(1){[0] =&gt; string(10)“subscriber”} [“allcaps”] =&gt;   array(3){[“read”] =&gt; bool(true)[“level_0”] =&gt;布尔(真)   [ “订户”] =&GT; bool(true)} [“filter”] =&gt; NULL}}

我如何回应此ARRAY1 [id]?

感谢GWC

我是这样做的:

if ((ctype_alnum($_GET["key"])) && (strlen($_GET["key"]) == 32)) {

$lh_users = get_users( array(
"meta_key" => "lh_scuritycode",
"meta_value" => $_GET["key"],
"number" => 1, "fields" => "ID"
) );

$this_user = $lh_users[0];

echo $this_user;

1 个答案:

答案 0 :(得分:4)

如果你想要的只是一个用户ID列表:

$lh_users = get_users( array(
                "meta_key" => "lh_scuritycode",
                "meta_value" => $_GET["key"],
                "fields" => "ID"
            ) );

echo '<li>' . implode( '</li><li>', $lh_users ) . '</li>';