get_user_by() WordPress Function

The get_user_by() function is used to retrieve a user object by a given field. The field can be either the user ID, the user login, or the user email address. This function is useful when you need to retrieve a user object but only have a limited amount of information about the user.

get_user_by( string $field, int|string $value ) #

Retrieve user info by a given field


Parameters

$field

(string)(Required)The field to retrieve the user with. id | ID | slug | email | login.

$value

(int|string)(Required)A value for $field. A user ID, slug, email address, or login name.


Top ↑

Return

(WP_User|false) WP_User object on success, false on failure.


Top ↑

Source

File: wp-includes/pluggable.php

	function get_user_by( $field, $value ) {
		global $current_user;

		$userdata = WP_User::get_data_by( $field, $value );

		if ( ! $userdata ) {
			return false;
		}

		if ( $current_user instanceof WP_User && $current_user->ID === (int) $userdata->ID ) {
			return $current_user;
		}

		$user = new WP_User;
		$user->init( $userdata );

		return $user;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.8.0Returns the global $current_user if it's the user being fetched.
4.4.0Added 'ID' as an alias of 'id' for the $field parameter.
2.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More