get_post_field

函数


get_post_field ( $field, $post = null, $context = 'display' )
参数
  • (string)
    $field
    Post field name.
    Required:
  • (int|WP_Post)
    $post
    Optional. Post ID or post object. Defaults to global $post.
    Required:
    Default: null
  • (string)
    $context
    Optional. How to filter the field. Accepts ‘raw’, ‘edit’, ‘db’, or ‘display’. Default ‘display’.
    Required:
    Default: ‘display’
返回值
  • (string) The value of the post field on success, empty string on failure.
相关
  • sanitize_post_field()
定义位置
  • wp-includes/post.php
    , line 1079
引入
2.3.0
弃用

根据帖子ID从一个文章字段中检索数据。

文章字段的例子有:”post_type”、”post_status”、”post_content “等,并基于文章对象的属性或键名。

上下文的值是基于分类法的过滤功能,支持的值可以在这些功能中找到。

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