You can use get_page_by_title()
or get_page_by_path()
.
By default, both return a page’s WP_Post
object, from which you can extract data. If the page doesn’t exist, they’ll return null
.
You can also use them on post types other than the built-in page
.
// By slug.
$slug = 'sample-page';
$page_by_slug = get_page_by_path( $slug );
// By title.
$title="Sample Page";
$page_by_title = get_page_by_title( $title );