WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get page by title

Description

Retrieves a post given its title. If more than one post uses the same title, the post with the smallest ID will be returned.

Because this function uses the MySQL '=' comparison the $page_title will usually be matched as case insensitive with default collating.

Usage


<?php 
  get_page_by_title
$page_title$output$post_type );
?>

Parameters

$page_title
(string) (required) Page title
Default: None
$output
(string) (optional) Output type. OBJECT, ARRAY_N, or ARRAY_A.
Default: OBJECT
$post_type
(string) (optional) Post type.
Default: page

Return Values

(mixed) 
OBJECT, ARRAY_N, or ARRAY_A.

NULL when no posts found.

Examples

Find Page ID to use with exclude in wp_list_pages

This example will return the $page object for the page titled "About". Then the $page->ID element is used to exclude the About page when listing pages.

<?php 
$page = get_page_by_title( 'About' );
wp_list_pages( 'exclude=' . $page->ID );
?>

How To Find WordPress Page ID By Title Then Replace the_content()

In this example, we find the page id of "Sample Page" then replace the page's the_content() with "Hello World!"

function my_content($content) {
    $page = get_page_by_title( 'Sample Page' );
    
    if ( is_page($page->ID) )
        $content = "Hello World!";
        return $content;
}

add_filter('the_content', 'my_content');

How to Find WordPress Custom Post Type by Title

This is useful for Custom Post Types. Below, we find the $post array of a Custom Post Type "link" with a title of "World Peace Now":

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

Notes

  • Uses global: (object) $wpdb

Change Log

  • Since: 2.1.0
  • 3.0.0: $post_type Parameter was added.

Source File

get_page_by_title() is located in wp-includes/post.php.

Related

Page Tags: get_all_page_ids(), get_ancestors(), get_page(), get_page_link(), get_page_by_path(), get_page_by_title(), get_page_children(), get_page_hierarchy(), get_page_uri(), get_pages(), is_page(), page_uri_index(), wp_list_pages(), wp_page_menu()

See also index of Function Reference and index of Template Tags.