Using WordPress ‘get_page_by_title()’ PHP function

The get_page_by_title() WordPress PHP function retrieves a page given its title.

Usage

get_page_by_title('Example Page', OBJECT, 'page');

Parameters

  • $page_title (string): Required. The title of the page.
  • $output (string): 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.
  • $post_type (string|array): Optional. Post type or array of post types. Default ‘page’. Default: ‘page’.

More information

See WordPress Developer Resources: get_page_by_title

Note: This function may return posts with any post_status, including those in the trash.

Examples

Find a Page by Title

This example retrieves the WP_Post object of a page titled “About Us”:

$page = get_page_by_title('About Us');
echo $page->ID;

Find a Custom Post Type by Title

This example finds a custom post type ‘portfolio’ with the title ‘My Work’:

$portfolio_post = get_page_by_title('My Work', OBJECT, 'portfolio');
echo $portfolio_post->ID;

Find a Page by Title and Return as Associative Array

This example retrieves the page titled “Services” as an associative array:

$page_array = get_page_by_title('Services', ARRAY_A);
print_r($page_array);

Find a Page by Title and Return as Numeric Array

This example retrieves the page titled “Contact” as a numeric array:

$page_numeric = get_page_by_title('Contact', ARRAY_N);
print_r($page_numeric);

Exclude a Page from wp_list_pages

This example excludes the “About” page when listing pages:

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