WordPress at a glance

get_page_by_title() WP 2.1.0

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.

Notes:

  • Global. wpdb. $wpdb WordPress database abstraction object.
✈ 1 time = 0.0015s = very slow | 50000 times = 19.42s = 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

#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://wpsite.ru/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; 

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

#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' );

Code of get_page_by_title: wp-includes/post.php VER 4.9.8

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

Related Functions

From tag: get_by

More from tag: query

More from category: Queries

No comments
    Hello, !     Log In . Register