函数原型
get_post( int|WP_Post|null $post = null, string $output = OBJECT, string $filter = ‘raw’ ): WP_Post|array|null
函数描述
Retrieves post data given a post ID or post object.
是否弃用
未弃用
函数参数
-
$post
int|WP_Post|null
Optional - Post ID or post object.
null,false,0and other PHP falsey values return the current global post inside the loop. A numerically valid post ID that points to a non-existent post returnsnull. Defaults to global $post.Default:
null -
$output
string
Optional - The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Post object, an associative array, or a numeric array, respectively.
Default:
OBJECT -
$filter
string
Optional - Type of filter to apply. Accepts
'raw','edit','db', or'display'. Default'raw'.Default:
'raw'
函数返回值
WP_Post|array|null Type corresponding to $output on success or null on failure.
When $output is OBJECT, a WP_Post instance is returned.
函数位置
File: wp-includes/post.php.
函数源码
function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) {
if ( empty( $post ) && isset( $GLOBALS['post'] ) ) {
$post = $GLOBALS['post'];
}
if ( $post instanceof WP_Post ) {
$_post = $post;
} elseif ( is_object( $post ) ) {
if ( empty( $post->filter ) ) {
$_post = sanitize_post( $post, 'raw' );
$_post = new WP_Post( $_post );
} elseif ( 'raw' === $post->filter ) {
$_post = new WP_Post( $post );
} else {
$_post = WP_Post::get_instance( $post->ID );
}
} else {
$_post = WP_Post::get_instance( $post );
}
if ( ! $_post ) {
return null;
}
$_post = $_post->filter( $filter );
if ( ARRAY_A === $output ) {
return $_post->to_array();
} elseif ( ARRAY_N === $output ) {
return array_values( $_post->to_array() );
}
return $_post;
}
源码链接
变更日志
| Version | Description |
|---|---|
| 1.5.1 | Introduced. |

