wppaste
WordPress

get_the_author_meta( string $field = '', int|false $user_id = false ): string

Since
2.8.0, 6.9.0
Source
wp-includes/author-template.php:167

Reads a single field from the WordPress user object for a post's author, falling back to the global $authordata when no $user_id is passed. Useful for pulling things like email, display_name, or user_registered without loading a full user object yourself. Every value passes through the dynamic get_the_author_{$field} filter before it's returned, so plugins can intercept or reformat it.

Retrieves the requested data of the author of the current post.

Description

Valid values for the $field parameter include:

  • admin_color
  • comment_shortcuts
  • description
  • display_name
  • first_name
  • ID
  • last_name
  • nickname
  • plugins_last_view
  • plugins_per_page
  • rich_editing
  • syntax_highlighting
  • user_activation_key
  • user_description
  • user_email
  • user_firstname
  • user_lastname
  • user_level
  • user_login
  • user_nicename
  • user_pass
  • user_registered
  • user_status
  • user_url

Compatibility

WordPress
since 6.9.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

$fieldstringoptional
The user field to retrieve. Default empty.Default: ''
$user_idint|falseoptional
User ID. Defaults to the current post author.Default: false

Return value

string
The author's field from the current author's DB object, otherwise an empty string.

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.

Show the post author's email address inside the loop

Query a single post and print the author's stored email while the loop sets the global $authordata for that post.

$author_query = new WP_Query( array( 'p' => 1, 'post_type' => 'post' ) );

if ( $author_query->have_posts() ) {
	while ( $author_query->have_posts() ) {
		$author_query->the_post();
		echo esc_html( get_the_author_meta( 'email' ) );
	}
}

wp_reset_postdata();

Outside a loop that has called the_post(), $authordata is unset and every field returns an empty string.

Look up a specific user's display name by ID without the loop

Pass a $user_id directly to skip $authordata entirely and pull the field straight from get_userdata().

$name = get_the_author_meta( 'display_name', 1 );

echo esc_html( $name );

This works even outside The Loop because supplying $user_id makes the function call get_userdata() instead of relying on the global.

Common problems and fixes · 4

Why does get_the_author_meta return an empty string when I call it outside the loop?

With no $user_id argument, the function falls back to the global $authordata, which is only populated by the_post() while a loop is running. Outside that context $authordata is unset and the null coalescing lookup resolves to an empty string.

Why does passing 'login' or 'email' work when the database column is really user_login and user_email?

The function checks $field against a short list (login, pass, nicename, email, url, registered, activation_key, status) and silently prepends 'user_' before reading the property, so both the short and full column names work for those particular fields.

How do I change the value that get_the_author_meta returns for a specific field sitewide?

Every return value passes through a dynamic filter named get_the_author_{$field}, where $field is the resolved property name (so it fires as get_the_author_user_email for the email alias, not get_the_author_email).

Do I need to escape what get_the_author_meta returns before printing it?

The function returns the raw property value from the user object with no HTML escaping applied, matching what apply_filters() passed through.

Alternatives and related functions

the_author_meta
When you want to echo the field directly in a template instead of getting a string back to work with.
get_the_author
When you only need the author's display name for the current post and don't want to specify a field.
get_userdata
When you need the entire WP_User object for a known user ID rather than one field at a time.
get_user_meta
When the value you want is custom user meta rather than a core column on the wp_users table.

Performance profile

How much work a call to get_the_author_meta() 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_user_by().

Scaling
Constant

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

Instructions
22–28

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

Plugin surface
1 hook

Third-party callbacks on 'get_the_author_{$field}' run inside this call, and their cost is not bounded by anything here.

Called by
42

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

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_user_by()one call below get_the_author_meta()

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_the_author_meta() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always22–25get_userdata(), apply_filters()
always24–28apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev3422–284
8.53422–284
8.43422–284
8.33422–284
8.23422–284
8.13422–2842 fewer instructions than PHP 7.4
7.43622–304

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.

Hooks and filters fired · 1

One hook fires while get_the_author_meta() runs, in this order:

  1. apply_filters( get_the_author_{$field} )filterline 195 (+28 into the body)

    Filters the value of the requested user metadata.

Uses · 2

Used by · 42

Show all 42

Source code

function get_the_author_meta( $field = '', $user_id = false ) {	$original_user_id = $user_id; 	if ( ! $user_id ) {		global $authordata;		$user_id = isset( $authordata->ID ) ? $authordata->ID : 0;	} else {		$authordata = get_userdata( $user_id );	} 	if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ), true ) ) {		$field = 'user_' . $field;	} 	$value = isset( $authordata->$field ) ? $authordata->$field : ''; 	/**	 * Filters the value of the requested user metadata.	 *	 * The filter name is dynamic and depends on the $field parameter of the function.	 *	 * @since 2.8.0	 * @since 4.3.0 The `$original_user_id` parameter was added.	 *	 * @param string    $value            The value of the metadata.	 * @param int       $user_id          The user ID for the value.	 * @param int|false $original_user_id The original user ID, as passed to the function.	 */	return apply_filters( "get_the_author_{$field}", $value, $user_id, $original_user_id );}

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.

6.9.0
Removed aim, jabber, and yim as valid values for the $field parameter.from the docblock
2.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/author-template.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.