A WordPress-centric search engine for devs and theme authors



get_page_by_title ›

Since2.1.0
Deprecatedn/a
get_page_by_title ( $page_title, $output = OBJECT, $post_type = 'page' )
Parameters: (3)
  • (string) $page_title Page title
    Required: Yes
  • (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: No
    Default: OBJECT
  • (string|array) $post_type Optional. Post type or array of post types. Default 'page'.
    Required: No
    Default: 'page'
Returns:
  • (WP_Post|array|null) WP_Post (or array) on success, or null on failure.
Defined at:
Codex:

Retrieve a page given its title.



Source

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 );
	}
}