0

I want to know if a particular page exists. I'd like to check against either it's slug or it's title.

There is a function called 'post_exists'. But this function seems to only work if it's invoked in hooks applicable to admin screens.

Is there a similar function that can be used in other contexts?

1 Answer 1

0

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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.