I am writing some code that will embed a chat bot to specified pages on my site. I have a function/shortcode that accepts 2 parameters, include and exclude. The value of each can be a single post ID or a comma-separated string of post IDs (that are later converted to arrays in the shortcode function). If both parameters are null (default), the bot will be embedded on every page of the site. If correct values are passed to either, the function will only add the bot to pages whose ID are in the include array and/or will NOT add it to pages with ID in the exclude array.

if (in_array($id, $exclude) || bene_get_pods_val('disable_chatbot') === true) return;
if ((count($include) === 0 && count($exclude) === 0) || in_array($id, $include)) {

On top of this I'd like to be able to exclude or include entire sections of pages (Products, Solutions, Company, Knowledge Base, etc) on the site. I know I can pass something to the shortcode telling it to include/exclude pages who are children of the 1 or more of the IDs passed. No problem there.

if( $include_children && $top_level_page_id === wp_get_post_parent_id(get_the_ID()) ) {
  return true;

My issue comes in with custom post types. For example, I have a Knowledge Base section that uses a regular post as its top-level page (i.e. the page located at www.example.com/knowledge-base/) but all of the content pages within that section are of a custom post type. For that reason, I cannot make them children of that top-level page, and therefore each of their parent page IDs returns 0.

I know I can check $_SERVER['REQUEST_URI'] for a specific slug

function pageIsInSection($top_level_page_id) {
  $is = false;
  $parent_slug = get_post_field('post_name', $top_level_page_id);
  if(false !== strpos($_SERVER['REQUEST_URI'], '/$parent_slug/')) {
    $is = true;
  return $is;

but that seems like a hack. Is there a better way to find out if the current page is a Knowledge Base (or any other section with custom post types) page?

1 Answer 1


I won't give a solution to your actual issue but I will show you how you can verify if current page is child of a specific top level page. If you need to check the current page has specific Parent page the you can do like this:

    $currentPage = get_the_id();
    $pageAncestors = get_post_ancestors($currentPage);

    $hasParent = in_array($currentPage,$pageAncestors);

here get_post_ancestors will return all parent id of that specific page as array and using in_array you can validate if id exists. Read more about get_post_ancestors here.

  • Sabbir, the problem I'm still having is with custom post types when the parent post is of post type page and the other pages in that section are of the custom post type docs. For example, the post type of the page at /knowledge-base/ is page, and the post type of the page at /knowledge-base/logging-in/ is docs (a custom post type). I can't set set the parent of a page of post type docs to a page of post type page. Make sense?
    – Daveh0
    Feb 9, 2021 at 7:30

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.