How to Check if a WordPress Page Is a Child Page (Subpage) and Get Its Parent

WordPress stores the parent of every page, so checking for a subpage takes one line. Here is that line, plus checks for a specific parent, any ancestor and sibling menus.

To check whether the current WordPress page is a child page, test its parent ID: if (is_page() && wp_get_post_parent_id(get_the_ID())) { ... }. A page with no parent returns 0. To check for a specific parent anywhere up the tree, use in_array($parent_id, get_post_ancestors(get_the_ID())). No custom function is needed, since WordPress stores the parent of every page in post_parent.

Originally published in March 2011 with a custom helper function. Rewritten in December 2026 with the built-in functions.

Is this a subpage?

if (is_page() && wp_get_post_parent_id(get_the_ID())) {
    // This page has a parent
}

Inside the loop, you can also read the property directly:

global $post;
if ($post->post_parent) {
    $parent_id = $post->post_parent;
}

Is it a child of a specific page?

Direct child only:

$services = 12; // ID of the "Services" page
if (wp_get_post_parent_id(get_the_ID()) === $services) {
    // directly under Services
}

Any level below it (child, grandchild and so on):

if (in_array($services, get_post_ancestors(get_the_ID()), true)) {
    // somewhere under Services
}

Hard-coding IDs breaks when content moves between staging and live. Look up the page by its path instead:

$services = get_page_by_path('services');
$is_under_services = $services && in_array($services->ID, get_post_ancestors(get_the_ID()), true);

Show the parent title and link

$parent_id = wp_get_post_parent_id(get_the_ID());
if ($parent_id) {
    printf('<a href="%s">&larr; %s</a>', esc_url(get_permalink($parent_id)), esc_html(get_the_title($parent_id)));
}

List sibling pages in a sidebar

A common use for this check is a section menu that shows the parent’s children:

$top = wp_get_post_parent_id(get_the_ID()) ?: get_the_ID();
wp_list_pages([
    'child_of' => $top,
    'title_li' => '',
]);

On a parent page, this lists its children. On a child page, it lists the child and its siblings.

Block themes

In a block theme you can show a section menu without code. The Page List block has a “parent page” option that lists the children of a chosen page.

Common questions

Does this work for custom post types?

Yes, for hierarchical post types that have 'hierarchical' => true. Non-hierarchical types like normal posts always have a parent of 0.

How do I get the top-level parent?

The last item from get_post_ancestors() is the top-level page: $ancestors = get_post_ancestors(get_the_ID()); $top = $ancestors ? end($ancestors) : get_the_ID();.

Can I use a different template for child pages?

Yes. Use the page_template filter and return a different template file when the page has a parent, or assign a template to child pages in the editor.