检索给定标题的页面。

原型

get_page_by_title( string $page_title, string $output = OBJECT, string|array $post_type = 'page' )

参数

$page_title (string) (Required) 页面标题

$output (string) (Optional)  所需的返回类型。 OBJECT,ARRAY_A或ARRAY_N之一,分别对应于WP_Post对象,关联数组或数值数组。

$post_type (string|array) (Optional)  帖子类型或帖子类型数组。

返回值

(WP_Post|array|null)  

源文件

路径:wp-includes/post.php

<?php
...
function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
	global $wpdb;
	if ( is_array( $post_type ) ) {
		$post_type = esc_sql( $post_type );
		$post_type_in_string = "'" . implode( "','", $post_type ) . "'";
		$sql = $wpdb->prepare( "
			SELECT ID
			FROM $wpdb->posts
			WHERE post_title = %s
			AND post_type IN ($post_type_in_string)
		", $page_title );
	} else {
		$sql = $wpdb->prepare( "
			SELECT ID
			FROM $wpdb->posts
			WHERE post_title = %s
			AND post_type = %s
		", $page_title, $post_type );
	}
	$page = $wpdb->get_var( $sql );
	if ( $page ) {
		return get_post( $page, $output );
	}
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/get_page_by_title/