get_post
函数
get_post ( $post = null, $output = OBJECT, $filter = 'raw' )
- 参数
-
-
(int|WP_Post|null)
$post
Optional. Post ID or post object. `null`, `false`, `0` and other PHP falsey values return the current global post inside the loop. A numerically valid post ID that points to a non-existent post returns `null`. Defaults to global $post.- Required: 否
- Default: null
-
(string)
$output
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.- Required: 否
- Default: OBJECT
-
(string)
$filter
Optional. Type of filter to apply. Accepts ‘raw’, ‘edit’, ‘db’, or ‘display’. Default ‘raw’.- Required: 否
- Default: ‘raw’
-
(int|WP_Post|null)
- 返回值
-
- (WP_Post|array|null) Type corresponding to $output on success or null on failure. When $output is OBJECT, a `WP_Post` instance is returned.
- 定义位置
-
-
wp-includes/post.php
, line 991
-
wp-includes/post.php
- 引入
- 1.5.1
- 弃用
- –
Retrieves post data given a post ID or post object.
See sanitize_post() for optional $filter values. Also, the parameter
`$post`, must be given as a variable, since it is passed by reference.
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;
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。