Checking for existence of a page by title?

Using get_page_by_title() along the line of below exemplary code should work:

if ( get_page_by_title( $page_title, $output, $post_type ) == NULL ) {
    $exists = false;
} else {
    $exists = true;
}

Explanation:

  • $page_title is obviously what you are looking for;
  • $output can be OBJECT, ARRAY_N or ARRAY – Default: OBJECT;
  • $post_type can be specified, in your case its not needed, because the default value is page;
  • Returns NULL if no post with the title is found;
  • For the inner logic take a look at the source.

Leave a Comment