Get page id by title?

There is a function exactly for that:

get_page_by_title( $page_title, $output = OBJECT, $post_type="page" );

You can use it like this:

$page = get_page_by_title( 'Start' );

To get the page from a specific post type:

$custom = get_page_by_title( 'Start', OBJECT, 'your_custom_post_type' );
$post   = get_page_by_title( 'Start', OBJECT, 'post' );

Be aware, this function will search in all post statuses. So you might get a draft, a trashed or private post. You should check the result with:

$status = get_post_status( $page );

if ( 'publish' !== $status )
    return; // do not show unpublished posts

A related function is get_page_by_path():

$page = get_page_by_path( 'about/contact' );

Leave a Comment