How to retrieve WordPress user ID by email address
To get a user ID by email in WordPress, you can use the get_user_by
function. Here’s a basic example of how to do it:
$email = 'user@example.com'; //The email address you're searching for
$user = get_user_by('email', $email);
if ( $user ) {
$user_id = $user->ID;
echo 'User ID: ' . $user_id;
} else {
echo 'User not found';
}
Replace user@example.com
with the email address you are looking up.get_user_by('email', $email)
retrieves the user object based on the email.
If user is found $user->ID
provides the user ID.
If no user is found with that email, it will output “User not found”.