get_user_by( string $field, int|string $value ): WP_User|false
- Since
- 2.8.0, 4.4.0
- Source
wp-includes/pluggable.php:101
Looks up a single WP_User account by matching one field (id, slug, email, or login) against a given value. Returns a WP_User object on success or false when no account matches, so check the result before calling methods on it. For ID-only lookups, get_userdata() is a thinner wrapper around the same query.
Compatibility
- WordPress
- since 4.4.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$fieldstring- The field to retrieve the user with. id | ID | slug | email | login.
$valueint|string- A value for $field. A user ID, slug, email address, or login name.
Return value
WP_User|false- WP_User object on success, false on failure.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Get a user by email address
Create a second user account and then look it up by email to confirm it exists before sending it a notification.
$new_user_id = wp_insert_user( array(
'user_login' => 'reviewer',
'user_email' => '[email protected]',
'user_pass' => wp_generate_password(),
) );
$user = get_user_by( 'email', '[email protected]' );
if ( $user ) {
echo esc_html( 'Found user: ' . $user->display_name . ' (ID ' . $user->ID . ')' );
} else {
echo esc_html( 'No user found with that email.' );
}wp_insert_user() runs once for this example; in real code you would look up an account someone already registered.
Look up the currently logged-in user by ID
Fetch the ID of the logged-in administrator and re-fetch the full WP_User object from it.
$current_id = get_current_user_id();
$user = get_user_by( 'id', $current_id );
if ( $user ) {
echo esc_html( 'Logged in as: ' . $user->user_login . ', role: ' . implode( ', ', $user->roles ) );
} else {
echo esc_html( 'No user is logged in.' );
}Common problems and fixes · 3
- Why does get_user_by return false even though the account exists?
- Can I pass an array to look up several users at once?
- Is it safe to pass a raw $_GET or $_POST value as $value?
Why does get_user_by return false even though the account exists?
Can I pass an array to look up several users at once?
Is it safe to pass a raw $_GET or $_POST value as $value?
Alternatives and related functions
get_userdata- When you only ever look up a user by numeric ID, get_userdata() is a shorter call that does the same thing.
get_users- When you need a list of users matching criteria instead of a single account by one field.
wp_get_current_user- When you want the currently logged-in user rather than looking one up by ID, slug, email, or login.
WP_User_Query- When the lookup needs pagination, role filtering, or meta query conditions beyond a single field match.
Performance profile
How much work a call to get_user_by() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 9–15
- Plugin surface
- None
- Called by
- 44
Reaches the database via get_user_by().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 16.
Nothing here hands control to plugin code.
44 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_user_by()this function does it
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_user_by() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 9 | ::WP_User() |
| always | 15 | ::WP_User(), ->init() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 16 instructions, 9–15 executed per call, 1 branch. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 2
- WP_User::get_data_by()Returns only the main user fields.
- WP_User::__construct()Constructor.
Used by · 44
- WP_Automatic_Updater::send_debug_email()Prepares and sends an email of a full log of background update results, useful for debugging and geekery.
- WP_Automatic_Updater::send_email()Sends an email upon the completion or failure of a background core update.
- WP_Automatic_Updater::send_plugin_theme_email()Sends an email upon the completion or failure of a plugin or theme background update.
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
- WP_Query::get_queried_object()Retrieves the currently queried object.
- WP_REST_Templates_Controller::get_wp_templates_author_text_field()Returns a human readable text for the author of the template.
- WP_REST_Users_Controller::create_item()Creates a single user.
- WP_REST_Users_Controller::update_item()Updates a single user.
- _wp_get_entity_view_config_posttype_wp_template()Provides the view configuration for the `wp_template` post type.
- _wp_personal_data_handle_actions()Handle list table actions.
- check_comment()Checks whether a comment passes internal checks to be allowed to add.
- check_password_reset_key()Retrieves a user row based on password reset key and login.
Show all 44
- email_exists()Determines whether the given email exists.
- get_avatar_data()Retrieves default data about the avatar.
- get_object_subtype()Returns the object subtype for a given object ID of a specific type.
- get_pages()Retrieves an array of pages (or hierarchical post type items).
- get_profile()Retrieve user data based on field.
- get_user()Retrieves user info by user ID.
- get_user_by_email()Retrieve user info by email.
- get_user_details()Deprecated functionality to retrieve user information.
- get_user_id_from_string()Get a numeric user ID from either an email address or a login.
- get_user_locale()Retrieves the locale of a user.
- get_userdata()Retrieves user info by user ID.
- get_userdatabylogin()Retrieve user info by login name.
- is_site_admin()Determine if user is a site admin.
- is_user_spammy()Determines whether a user is marked as a spammer, based on user login.
- populate_network_meta()Creates WordPress network meta and sets the default values.
- retrieve_password()Handles sending a password retrieval email to a user.
- username_exists()Determines whether the given username exists.
- wp_authenticate_application_password()Authenticates the user using an application password.
- wp_authenticate_email_password()Authenticates a user using the email and password.
- wp_authenticate_username_password()Authenticates a user, confirming the username and password are valid.
- wp_create_user_request()Creates and logs a user request to perform a specific action.
- wp_media_personal_data_exporter()Finds and exports attachments associated with an email address.
- wp_new_user_notification()Emails login credentials to a newly-registered user.
- wp_notify_moderator()Notifies the moderator of the site about a new comment that is awaiting approval.
- wp_notify_postauthor()Notifies an author (and/or others) of a comment/trackback/pingback on a post.
- wp_password_change_notification()Notifies the blog admin of a user changing password, normally via email.
- wp_setcookie()Sets a cookie for a user who just logged in. This function is deprecated.
- wp_user_personal_data_exporter()Finds and exports personal data associated with an email address from the user and user_meta table.
- wp_validate_auth_cookie()Validates authentication cookie.
- wpmu_new_site_admin_notification()Notifies the Multisite network administrator that a new site was created.
- wpmu_signup_blog_notification()Sends a confirmation request email to a user when they sign up for a new site. The new site will not become active until the confirmation link is clicked.
- wpmu_signup_user_notification()Sends a confirmation request email to a user when they sign up for a new user account (without signing up for a site at the same time). The user account will not become active until the confirmation link is clicked.
Source code
function get_user_by( $field, $value ) { $userdata = WP_User::get_data_by( $field, $value ); if ( ! $userdata ) { return false; } $user = new WP_User(); $user->init( $userdata ); return $user; }Changelog
Introduced in 2.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$field parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/pluggable.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.