函数文档

get_post_field()

💡 云策文档标注

概述

get_post_field() 函数用于根据 Post ID 检索文章字段的数据,如 post_type、post_status 等。它支持通过上下文参数过滤字段值,并返回字段值或空字符串。

关键要点

  • 函数基于 Post ID 或文章对象获取指定字段的值,字段名区分大小写。
  • 参数包括必填的字段名、可选的 Post ID 或文章对象(默认为全局 $post),以及可选的上下文(如 'raw'、'edit'、'db'、'display')。
  • 返回值为整数、字符串或整数数组,失败时返回空字符串。
  • 内部调用 get_post() 获取文章对象,并使用 sanitize_post_field() 进行过滤。
  • 相关函数包括 sanitize_post_field() 和 get_post(),常用于文章数据处理场景。

代码示例

// 获取文章作者 ID 和显示名称(循环外)
$author_id = get_post_field( 'post_author', $post_id );
$author    = get_the_author_meta( 'display_name', $author_id );

// 字段名区分大小写示例
get_post_field('ID'); // 返回整数 ID,如 1035
get_post_field('id'); // 返回空字符串

注意事项

  • 字段名必须与文章对象属性名一致,如 'post_content'、'post_title',区分大小写。
  • 默认上下文为 'display',可根据需要选择其他上下文以获取原始或编辑数据。
  • 函数在 4.5.0 版本中使 $post 参数可选,2.3.0 版本引入。

📄 原文内容

Retrieves data from a post field based on Post ID.

Description

Examples of the post field will be, ‘post_type’, ‘post_status’, ‘post_content’, etc and based off of the post object property or key names.

The context values are based off of the taxonomy filter functions and supported values are found within those functions.

See also

Parameters

$fieldstringrequired
Post field name.
$postint|WP_Postoptional
Post ID or post object. Defaults to global $post.

Default:null

$contextstringoptional
How to filter the field. Accepts 'raw', 'edit', 'db', or 'display'. Default 'display'.

Return

int|string|int[] The value of the post field on success, empty string on failure.

Source

function get_post_field( $field, $post = null, $context = 'display' ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return '';
	}

	if ( ! isset( $post->$field ) ) {
		return '';
	}

	return sanitize_post_field( $field, $post->$field, $post->ID, $context );
}

Changelog

Version Description
4.5.0 The $post parameter was made optional.
2.3.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    Here are the default post fields that you can get (case-sensitive):

    ID
    post_author
    post_date
    post_date_gmt
    post_content
    post_title
    post_excerpt
    post_status
    comment_status
    ping_status
    post_password
    post_name
    to_ping
    pinged
    post_modified
    post_modified_gmt
    post_content_filtered
    post_parent
    guid
    menu_order
    post_type
    post_mime_type
    comment_count
    filter

  2. Skip to note 8 content

    Here is a simple one-liner to get a formatted first line of the written post content. Useful for meta page description (demo’d here), excerpts and the like.

    $description = ucfirst( preg_split('/[.?!]/',  wp_strip_all_tags( get_post_field( 'post_content', '' ), true ), 2, )[0] ) . '.';
    echo <<<EOF
    <meta name="description" content="$description">
    EOF;

    Breakdown:

    ucfirst( $mystring )
    PHP Function: Uppercase the first character (Eng sentence case).

    preg_split('/[.?!]/', $mystring , 2, )
    PHP Function: Split string at regex defined chars, limit to n chunks (could return more than one sentence).

    wp_strip_all_tags( $myhtml , true )
    WP Function: Get the HTML gone, bool: remove breaks.

    get_post_field( 'post_content', '' )
    WP Function: Get the post content.

    . '.'
    Notice I append a “.” as preg_split disposes of the target char. If you require the actual punctuation, e.g “!?” you will need to expand this.