How to Get Page by Title in WordPress (+Example)

2.7777777777778 from 5
from 9 user
(Rate this post)
How to Get Page by Title in WordPress (+Example)

Getting full details of a page by title is not that challenging in WordPress. WP has a function named get_page_by_title that is useful when a developer wants to retrieve page data from a specified title. I will try this function inside a working example for those interested developers.

get_page_by_title function Is useful when a WordPress developer wants full details of a page, but he or she only has the title available.

How to use get_page_by_title function to find a page by title?

get_page_by_title syntax in WordPress

The get_page_by_title function receives 3 parameters. You must specify a title, and two optional parameters for output, and post type.

get_page_by_title($pageTitle, $output = OBJECT, $postType = 'page')
  • $pageTitle: This parameter is required. Set the title to get it if it’s available.
  • $output: Can be one of OBJECT (default value), ARRAY_A (associative array), or ARRAY_N (numeric array) values.
  • $postType: Default value for this parameter is page, but you can set other types or an array of types.

Example of getting a page using get_page_by_title function

I want to retrieve a page with “About Us” title, although you can replace it with anything you want:

$pageTitle = "About Us";
$pageObject = get_page_by_title($pageTitle);
if($pageObject){
    //page exists, use the object

}

Result of running get_page_by_title function

The function will return a WP_Post object of the page if it’s available. otherwise, it will return a null value:

WP_Post Object
(
    [ID] => 140
    [post_author] => 1
    [post_date] => 2021-02-05 11:53:37
    [post_date_gmt] => 2021-02-05 11:53:37
    [post_content] => Test Content
    [post_title] => About Us
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] => 
    [post_name] => about-us
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2021-02-05 11:53:37
    [post_modified_gmt] => 2021-02-05 11:53:37
    [post_content_filtered] => 
    [post_parent] => 0
    [guid] => http://sitename.com/about-us/
    [menu_order] => 0
    [post_type] => page
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => raw
)

Good questions about get_page_by_title function

You may have questions about the WordPress get_page_by_title function.

Here I answered a few important questions that I’ve encountered about this function:

1- What page the get_page_by_title function will return when two or more pages have the same title?

The get_page_by_title function will return the older one when two pages have the same title.

If in a case, it’s just two pages with the same title, the get_page_by_title function will the one with the smaller ID.

But if it’s multiple pages with the same title (more than two), WordPress uses publication date to determine the oldest page, not the ID.

2- Is the get_page_by_title function case sensitive?

Yes, It is!

You better set the exact title to find a page when you are using the get_page_by_title function.

3- What if I need to get pages based on other information?

The get_posts function in WordPress is useful when you need pages with other information.

I recommend you read my article about get_posts function:

WordPress Query Posts by Category, Tag, Custom attribute, and more!

When using the get_posts function, you can use the post_type field to find posts of any type (in this case, page).

4- Where can we learn more about WordPress pages and their related functions?

WordPress’s official website has a developers section that is the best source for any information when you are a beginner.

Search for page functions inside the WordPress archive, so you can find many useful functions related to WordPress pages.

What do you think about "How to Get Page by Title in WordPress (+Example)"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu