Recently I needed to find a user on my WordPress site with a specific member number that had been assigned at registration. This member number was a custom user meta field that I had created. I wanted to find all the user’s information based on that member number.
I tried using the WordPress ‘get_users’ function but that only returns the WordPress meta and not the custom meta. I ended up using the ‘get_users’ function to find the user ID and then using the ‘get_user_meta’ function to get the rest of the information.
Since the ‘get_user_meta’ function returns arrays, I used the php array_map function to retrieve the values.
$member_id = '1234567889999';
if ($member_arr = get_users(array('meta_key' => 'member_id', 'meta_value' => $member_id, 'fields' => 'ID'))) {
$user_id = $member_arr[0];
if ($userdata = get_user_meta($user_id)) {
$userdata = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user_id ) );
print_r($userdata);
}
} else {
echo 'Member not found';
}
This code will return all information from the user’s profile including any custom meta fields:
Array
(
[first_name] => test
[last_name] => user
[nickname] => testuser
[description] =>
[rich_editing] => true
[comment_shortcuts] => false
[admin_color] => fresh
[use_ssl] => 0
[show_admin_bar_front] => true
[wp_capabilities] => a:1:{s:10:”subscriber”;b:1;}
[wp_user_level] => 0
[entry_id] => 1
[photo] => https://example.com/wp-content/uploads/gravity_forms/1-4ed7200bd442a04566979b2fc2751c06/2013/06/cindy.jpg
[travel_log] => Florida 9/2/2013
Maine 1/2/2014
[member_id] => 1234567889999
)
