函数原型
get_page_by_title( string $page_title, string $output = OBJECT, string|array $post_type = ‘page’ ): WP_Post|array|null
函数描述
Retrieves a page given its title.
是否弃用
未弃用
函数参数
-
$page_title
string
Required - Page title.
-
$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 -
$post_type
string|array
Optional - Post type or array of post types. Default
'page'.Default:
'page'
函数返回值
WP_Post|array|null WP_Post (or array) on success, or null on failure.
函数位置
File: wp-includes/post.php.
函数源码
function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
$args = array(
'title' => $page_title,
'post_type' => $post_type,
'post_status' => get_post_stati(),
'posts_per_page' => 1,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'no_found_rows' => true,
'orderby' => 'post_date ID',
'order' => 'ASC',
);
$query = new WP_Query( $args );
$pages = $query->posts;
if ( empty( $pages ) ) {
return null;
}
return get_post( $pages[0], $output );
}
源码链接
变更日志
| Version | Description |
|---|---|
| 3.0.0 | The $post_type parameter was added. |
| 2.1.0 | Introduced. |

