get_page_by_title()

最后更新于:2021-11-26 10:57:09

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

Retrieve 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 GC_Post object, an associative array, or a numeric array, respectively.

Default value: OBJECT

$post_type

(string|array) (Optional) Post type or array of post types.

Default value: ‘page’

响应

(GC_Post|array|null) GC_Post (or array) on success, or null on failure.

源文件

文件: gc-includes/post.php

function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
	global $gcdb;

	if ( is_array( $post_type ) ) {
		$post_type           = esc_sql( $post_type );
		$post_type_in_string = "'" . implode( "','", $post_type ) . "'";
		$sql                 = $gcdb->prepare(
			"
			SELECT ID
			FROM $gcdb->posts
			WHERE post_title = %s
			AND post_type IN ($post_type_in_string)
		",
			$page_title
		);
	} else {
		$sql = $gcdb->prepare(
			"
			SELECT ID
			FROM $gcdb->posts
			WHERE post_title = %s
			AND post_type = %s
		",
			$page_title,
			$post_type
		);
	}

	$page = $gcdb->get_var( $sql );

	if ( $page ) {
		return get_post( $page, $output );
	}
}