wppaste
WordPress

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.

Retrieves user info by a given field.

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?

The function returns false whenever WP_User::get_data_by() finds no matching row, which happens if $field is not exactly one of id, ID, slug, email, or login, or if $value has a typo, wrong case in the login, or extra whitespace from form input.

Can I pass an array to look up several users at once?

No. The function only accepts a single scalar $field and a single $value, and it always returns either one WP_User object or false, never a list.

Is it safe to pass a raw $_GET or $_POST value as $value?

The function will run the value through WP_User::get_data_by() straight into a database lookup, so unsanitized input can be used to probe which logins or emails exist on the site (a user enumeration risk), even though it will not cause SQL injection.

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

Reaches the database via get_user_by().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
9–15

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 16.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
44

44 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_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.

WhenInstructionsCalls it makes
always9::WP_User()
always15::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

Used by · 44

Show all 44

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

4.4.0
Added 'ID' as an alias of 'id' for the $field parameter.from the docblock
2.8.0
Introduced.from the docblock

About 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.