I want to find the user’s id based on a piece of user metadata that I have.
The metadata I have is coming in as part of the url.
My url looks something like this:
http://example.com/users/wu8shti
where ‘wu8shti’ is a member id.
Here is how I find the user’s ID:
//
$uri = explode("/",$_SERVER[REQUEST_URI]);
$member_id = $uri[2];
$args = array(
'meta_query' => array(
array(
'key' => 'member_id',
'value' => $member_id,
'compare' => '='
)
)
);
$member_arr = get_users($args); //finds all users with this meta_key == 'member_id' and meta_value == $member_id passed in url
if ($member_arr) { // any users found?
foreach ($member_arr as $user) { // in my case, there should only be one user with the id
$user_id = $user->ID; // so I just get the last one, which should be the only one
}
}
//
This code should be placed anywhere you need to access the user’s ID.
It could be in a function in the functions.php file or it could be in the page itself if you are using a plugin such as ‘php code’ which will allow you to use php code in the page.
Or you could just put it in a custom child page template.
Basically, anywhere you can use php code, you can use this in your WordPress site.
