get_page_by_title()WP 2.1.0

Deprecated from version 6.2.0. It is no longer supported and can be removed in future releases. Use WP_Query instead.

Retrieve any type of post (page, post, custom type) by given title.

If several posts with the same title are found, the post with the smaller ID will be returned.

The post of which type you need to get is specified in the third parameter $post_type. By default, the title will be searched for "static pages" (page).

Because the function uses = comparison in MySQL query, the header is not case sensitive in most cases. It depends on MySQL settings.

If you need to get the ID by URL, use a special function: url_to_postid().

It is not recommended to use this function everywhere. Before using this function, think about how it will work. Keep in mind that the query generated by this function can be very heavy, not like for example getting a post by ID. The function looks through an unindexed column of the wp_posts table, so it will have to check all rows that match the post type. Therefore, the default type is page, usually, there are no more than 20 records in this post type.

Since WP 6.1 uses WP_Query, which тщц cache the result.

1 time — 0.0015 sec (very slow) | 50000 times — 19.42 sec (slow) | PHP 7.0.2, WP 4.4.1

No Hooks.

Return

WP_Post|Array|null. WP_Post (or array) on success, or null on failure.

Usage

get_page_by_title( $page_title, $output, $post_type );
$page_title(string) (required)
The title of the post (page) the data of which you want to get.
$output(string)

The output format of the found post. It may be:

  • OBJECT - return as object;
  • ARRAY_N - return as a numbered array;
  • ARRAY_A - return as an associative array.

Default: OBJECT

$post_type(string/array)
Post type or array of post types.
Default: 'page'

Examples

0

#1 Get page data

There is a page with the title "About Me". We need to get the data object of this page (all data of the wp_posts table row in the database):

$ppp = get_page_by_title('About Me');

/*
$ppp будет содержать:

WP_Post Object
(
	[ID] => 7
	[post_author] => 1
	[post_date] => 2010-03-28 15:31:00
	[post_date_gmt] => 2010-03-28 11:31:00
	[post_content] => The content of the post. It usually says a lot. :)
	[post_title] => About Me
	[post_excerpt] => 
	[post_status] => publish
	[comment_status] => open
	[ping_status] => closed
	[post_password] => 
	[post_name] => about
	[to_ping] => 
	[pinged] => 
	[post_modified] => 2014-04-21 01:23:20
	[post_modified_gmt] => 2014-04-20 21:23:20
	[post_content_filtered] => 
	[post_parent] => 0
	[guid] => http://wpexample.com/about
	[menu_order] => 0
	[post_type] => page
	[post_mime_type] => 
	[comment_count] => 116
	[filter] => raw
)
*/

After receiving the object of the post we can get different data. For example, we can get the post ID by title:

$ppp = get_page_by_title('About Me');
echo $ppp->ID;           
echo $ppp->post_content; 
0

#2 Exclude the page from the list by title

Suppose we display the page menu. But we do not need to display a page with the title "Contacts" in this menu.

$page = get_page_by_title( 'Contacts' );
wp_list_pages( 'exclude=' . $page->ID );
0

#3 Find Custom Post Type by Title

Below, we find the $post array of a Custom Post Type "book" with a title of "World Peace Now":

$mypost = get_page_by_title( 'World Peace Now', OBJECT, 'book' );

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 2.1.0 Introduced.
Since 3.0.0 The $post_type parameter was added.
Deprecated since 6.2.0 Use WP_Query.

get_page_by_title() code WP 6.5.2

function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
	_deprecated_function( __FUNCTION__, '6.2.0', 'WP_Query' );
	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 );
	}

	return null;
}