函数文档

get_the_author_meta()

💡 云策文档标注

概述

get_the_author_meta() 函数用于检索当前文章作者的用户数据,支持多种字段参数,适用于程序化处理。在循环内使用时默认基于当前作者,循环外需指定用户ID。

关键要点

  • 函数返回作者字段的字符串值,若字段不存在则返回空字符串。
  • 参数 $field 可选,支持预定义字段如 admin_color、display_name、user_email 等,也支持插件添加的自定义用户元数据。
  • 参数 $user_id 可选,默认为 false,在循环内自动使用当前作者ID,循环外必须指定。
  • 与 the_author_meta() 区别:get_the_author_meta() 用于获取数据,the_author_meta() 用于直接输出。
  • 函数内部使用动态过滤器 apply_filters( "get_the_author_{$field}", $value, $user_id, $original_user_id ) 允许对返回值进行过滤。

代码示例

// 获取作者ID(循环内)
$author_id = get_the_author_meta('ID');

// 获取作者邮箱(指定用户ID)
$email = get_the_author_meta('user_email', 25);

// 使用 wpautop 格式化作者描述
echo wpautop(get_the_author_meta('description'));

注意事项

  • 在循环外使用时,必须通过全局 $post 或类似方式获取用户ID,例如:global $post; $author_id = $post->post_author;
  • 字段名如 'login' 会自动转换为 'user_login',但建议直接使用完整字段名以避免混淆。
  • 插件添加的用户元数据可通过字段键直接检索,扩展了函数功能。

📄 原文内容

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

Parameters

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

Default:false

Return

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

More Information

If used within The Loop, the user ID need not be specified, it defaults to current post author. A user ID must be specified if used outside The Loop.

get_the_author_meta() returns the data for use programmatically in PHP. To just display it instead, use the_author_meta()

If the specified meta field does not exist for this user, an empty string is returned.

Plugins may add additional fields to the user profile, which in turn adds new key/value pairs to the wp_usermeta database table. This additional data can be retrieved by passing the field’s key to the function as the $field parameter.

Source

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

Hooks

apply_filters( “get_the_author_{$field}”, string $value, int $user_id, int|false $original_user_id )

Filters the value of the requested user metadata.

Changelog

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

User Contributed Notes

  1. Skip to note 8 content

    Get the author ID Outside loop:

    global $post;
    $author_id = $post->post_author;

    Get the author ID inside a loop :

     $author_id = get_the_author_meta( 'ID' ); 

    bellow is a few examples of author value :

    // to get  nicename
    get_the_author_meta( 'nicename', $author_id );
    
    // to get  email
    get_the_author_meta( 'email', $author_id );
    
    // to get  url
    get_the_author_meta( 'url', $author_id );
    
    // to get  status
    get_the_author_meta( 'status', $author_id );

  2. Skip to note 10 content

    Using the wpautop() for description will also keep the line break (like in studio-jt’ comment) but will output cleaner html:

    /**
     * Display Author's description 
     * with keeping line breaks and wrapping its paragraphs
     * into <p> tag
     *
     * @link <a href="https://developer.wordpress.org/reference/functions/wpautop/" rel="ugc">https://developer.wordpress.org/reference/functions/wpautop/</a>
     */
    echo wpautop( get_the_author_meta( 'description' ) );