wppaste
WordPress

WP_User::get_data_by( string $field, string|int $value ): object|false

Since
3.3.0, 4.4.0
Source
wp-includes/class-wp-user.php:203
Returns only the main user fields.

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 query against: Accepts 'id', 'ID', 'slug', 'email' or 'login'.
$valuestring|int
The field value.

Return value

object|false
Raw user object.

Performance profile

How much work a call to WP_User::get_data_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
Heavy

Reaches the database via ->get_row().

Scaling
Constant

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

Instructions
10–62

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

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

What it touches

  • cacheobject cachewp_cache_get()called directly
  • sqldatabase query->get_row()called directly
  • hookthird-party callbacksapply_filters()one call below WP_User::get_data_by()

Further down the call graph this can also reach option, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 14 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_User::get_data_by() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always10–25none
$user_id !== false23–30wp_cache_get()
$user_id !== false27–38wp_cache_get(), wp_cache_get()
$user_id !== false31–44sanitize_user(), wp_cache_get(), wp_cache_get()
$user_id === false32–39->prepare(), ->get_row()
$user_id === false35–42->prepare(), ->get_row(), update_user_caches()
always36–47wp_cache_get(), ->prepare(), ->get_row()
always39–50wp_cache_get(), ->prepare(), ->get_row(), update_user_caches()
$user_id === false40–53sanitize_user(), wp_cache_get(), ->prepare(), ->get_row()
$user_id !== false42–53wp_cache_get(), wp_cache_get(), ->prepare(), ->get_row()
$user_id === false43–56sanitize_user(), wp_cache_get(), ->prepare(), ->get_row(), update_user_caches()
$user_id !== false45–56wp_cache_get(), wp_cache_get(), ->prepare(), ->get_row(), update_user_caches()
2 further outcomes, up to 62 instructions
$user_id !== false46–59sanitize_user(), wp_cache_get(), wp_cache_get(), ->prepare(), ->get_row()
$user_id !== false49–62sanitize_user(), wp_cache_get(), wp_cache_get(), ->prepare(), ->get_row(), update_user_caches()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8810–6213
8.58810–6213
8.48810–62134 fewer instructions than PHP 8.3
8.39212–6413
8.29212–6413
8.19212–64131 more instruction than PHP 7.4
7.49112–6413

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 · 3

Used by · 2

Source code

	public static function get_data_by( $field, $value ) {		global $wpdb; 		// 'ID' is an alias of 'id'.		if ( 'ID' === $field ) {			$field = 'id';		} 		if ( 'id' === $field ) {			// Make sure the value is numeric to avoid casting objects, for example, to int 1.			if ( ! is_numeric( $value ) ) {				return false;			}			$value = (int) $value;			if ( $value < 1 ) {				return false;			}		} else {			$value = trim( $value );		} 		if ( ! $value ) {			return false;		} 		switch ( $field ) {			case 'id':				$user_id  = $value;				$db_field = 'ID';				break;			case 'slug':				$user_id  = wp_cache_get( $value, 'userslugs' );				$db_field = 'user_nicename';				break;			case 'email':				$user_id  = wp_cache_get( $value, 'useremail' );				$db_field = 'user_email';				break;			case 'login':				$value    = sanitize_user( $value );				$user_id  = wp_cache_get( $value, 'userlogins' );				$db_field = 'user_login';				break;			default:				return false;		} 		if ( false !== $user_id ) {			$user = wp_cache_get( $user_id, 'users' );			if ( $user ) {				return $user;			}		} 		$user = $wpdb->get_row(			$wpdb->prepare(				"SELECT * FROM $wpdb->users WHERE $db_field = %s LIMIT 1",				$value			)		);		if ( ! $user ) {			return false;		} 		update_user_caches( $user ); 		return $user;	}

Changelog

Introduced in 3.3.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
3.3.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-user.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.