函数原型
get_post_field( string $field, int|WP_Post $post = null, string $context = ‘display’ ): string
函数描述
Retrieves data from a post field based on Post ID.
是否弃用
未弃用
函数参数
-
$field
string
Required - Post field name.
-
$post
int|WP_Post
Optional - Post ID or post object. Defaults to global $post.
Default:
null -
$context
string
Optional - How to filter the field. Accepts
'raw','edit','db', or'display'. Default'display'.Default:
'display'
函数返回值
string The value of the post field on success, empty string on failure.
函数位置
File: wp-includes/post.php.
函数源码
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 );
}
源码链接
变更日志
| Version | Description |
|---|---|
| 4.5.0 | The $post parameter was made optional. |
| 2.3.0 | Introduced. |

