WordPress at a glance

get_the_author_meta() WP 2.8.0

Retrieves the requested data of the current or requested user (post author).

No need to specify user ID if the function used inside The Loop, it returns the data of current post author, in this case. You need to specify the user ID only if the function is used outside The Loop.

To immediately display the data, use the_author_meta().

Some plugins can extend the user data (such data adds to wp_usermeta table specifying key & value). To get such new field value specify the name (key) of this new field.

Works based on: get_userdata()
1 time = 0.000001s = speed of light | 50000 times = 0.01s = speed of light | PHP 7.3.12, WP 5.3.2
Hooks from the function
Return

String/Array. User data, if it exists, otherwise an empty string.

Usage

get_the_author_meta( $field, $user_id );
$field(string)

The user field to retrieve. Can be:

ID
user_login          | login
user_pass           | pass
user_nicename       | nicename
user_email          | email
user_url            | url
user_registered     | registered
user_activation_key | activation_key
user_status         | status
user_description    | description (Биографические данные из профиля пользователя)
user_firstname      | first_name
user_lastname       | last_name
user_level          | $wpdb->prefix . 'user_level'
roles
display_name
nickname
rich_editing
comment_shortcuts
admin_color
plugins_per_page
plugins_last_view

Default: ''

$user_id(int/false)
User ID. If specify this parameter, then the function returns data of the specified user (author).
Default: false (current user/author data)

Examples

#1 Retrieve the user e-mail

Let's get the email of the current post author and place it in the $user_email variable to use it further in our code (remember that the function is retrieve the data. but not display it).

$user_email = get_the_author_meta( 'user_email' );
echo $user_email;

Note: if you retrieve the data on the separated single page, before the function call you need to call the_post() function, or you need to specify user ID in second parameter ($post->post_author).

#2 Retrieve user name and link to email.

Let's get e-mail of the user with ID = 25 and display his "display name":

<p>Write to the author's email: 
	<a href="mailto:<?php echo get_the_author_meta('user_email', 25); ?>">
		<?php the_author_meta('display_name', 25); ?>
	</a>
</p>

Notes

  • Global. WP_User. $authordata The current author's data.

Changelog

Since 2.8.0 Introduced.

Code of get_the_author_meta() WP 5.5.3

<?php
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 );
}

From tag: Metadatas (any)

No comments
    Log In