Wordpress - get the userID from custom user meta

5.1k views Asked by At

I try to get the wordpress USER ID from a userMeta key I created during the registration.

The user meta key is 'lh_scuritycode' with meta value $_GET["key"].

$_GET["key"] = f16dea7e995b14e420ddb96fb7dbd67f

This is my code:

<?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";   
}

?>

Now, $lh_users is only an array if I echo it. When I use the foreach it works. How can I do it without that foreach?

when I do this "var_dump" the result is:

array(1) { [0]=> object(WP_User)#126 (7) { ["data"]=> object(stdClass)#2077 (10) { ["ID"]=> string(2) "24" ["user_login"]=> string(7) "Aner" ["user_pass"]=> string(34) "$P$BDpMKv0" ["user_nicename"]=> string(7) "aner" ["user_email"]=> string(18) "infe" ["user_url"]=> string(0) "" ["user_registered"]=> string(19) "2013-0 1:02" ["user_activation_key"]=> string(0) "" ["user_status"]=> string(1) "0" ["display_name"]=> string(7) "Aner" } ["ID"]=> int(24) ["caps"]=> array(1) { ["subscriber"]=> bool(true) } ["cap_key"]=> string(22) "localhero_capabilities" ["roles"]=> array(1) { [0]=> string(10) "subscriber" } ["allcaps"]=> array(3) { ["read"]=> bool(true) ["level_0"]=> bool(true) ["subscriber"]=> bool(true) } ["filter"]=> NULL } }

And How do I echo this ARRAY1[id]?

Thanks to GWC

I do it this way:

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

There are 1 answers

1
gwc On BEST ANSWER

If all you want is a list of user IDs:

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

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