Check if a page is a parent or if it's a child page?

Check if a page is a parent or if it's a child page?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Determining Parent vs Child Page Relationships in WordPress

When building a dynamic navigation system, you often need to distinguish between a parent page and its children to render different menus or UI states. The standard approach involves checking the post_parent property of the current post object. However, hardcoding specific IDs like $post->post_parent == '100' creates brittle code that fails as soon as your content structure evolves.

To solve this dynamically without relying on hardcoded values, you need to evaluate the relationship contextually rather than numerically. The logic shifts from checking if a page equals a specific ID to checking if the current page has any children or if it belongs to a specific parent hierarchy.

Checking for Children Directly

The most robust way to determine if the current page is a parent is to inspect whether it possesses descendants. You can achieve this by querying the database directly using the children argument in a WP_Query, or by leveraging helper functions available in modern WordPress versions.

If you are working within the context of a single page (e.g., inside single.php or a template part), the simplest method is to use the get_parent() function combined with a query for children. However, WordPress provides a cleaner abstraction through the children parameter when constructing a query on the current post.

Here is a practical implementation using WP_Query:

$children_query = new WP_Query(array(
    'post_type'      => get_post_type($post->ID),
    'child_of'       => $post->ID, // Query for children of the current page
    'posts_per_page' => -1         // Retrieve all children to check existence
));

if ($children_query->have_posts()) {
    // The current page is a parent
    render_parent_menu();
} else {
    // The current page is a child (or a standalone root)
    render_child_menu();
}

This approach avoids hardcoding IDs entirely. It simply asks the database: "Does this post have any children?" If the query returns posts, the current page is a parent.

Checking for Parentage Without Hardcoding

Conversely, determining if the current page is a child requires knowing its parent's ID. Since get_post() does not automatically fetch the parent in the current context, you must retrieve the parent ID first.

You can use get_post($post->ID, 'ARRAY_S') or simply access $post->post_parent. If this value is greater than zero, the page has a parent and is therefore a child page.

$current_id = get_the_ID();
$parent_id  = get_post_field('post_parent', $current_id);

if ($parent_id > 0) {
    // The current page is a child of another page
    render_child_menu();
} else {
    // The current page is a root/parent page (no parent exists)
    render_parent_menu();
}

This logic covers the vast majority of scenarios. A page with post_parent set to 0 is either a root page or an orphaned child (though orphaned children are rare in well-maintained sites).

Handling Hierarchies and Nested Menus

Your requirement mentions displaying different menus based on hierarchy levels. Simply checking if a page is a parent might not be enough if you need to distinguish between a top-level parent and a sub-parent.

In such cases, you can check the depth of the hierarchy by comparing the current ID against its grandparent. If get_post_field('post_parent', $current_id) exists but does not match the page's own grandparent (obtained via recursive lookup or custom function), the page is a direct child of a top-level parent.

Alternatively, if you are using a theme framework or a page builder, these tools often provide specific variables like is_child() or has_children(). Always check your theme documentation first, as native functions might be overridden by custom hooks.

Performance Considerations

While the examples above use WP_Query to check for children, this is generally fine for simple checks. However, running a query inside the loop or on high-traffic pages can impact performance if not optimized.

For better performance in critical paths, consider caching the result of the parent check. WordPress stores post data in global variables when using the Loop; accessing $post->post_parent is an O(1) operation and does not require a database hit. The WP_Query approach for checking children is only necessary if you need to know which children exist or iterate over them, not just their existence.

If you frequently need this logic, encapsulate it in a reusable function placed in your theme's functions.php:

function is_current_page_parent() {
    $children_query = new WP_Query(array(
        'child_of'   => get_the_ID(),
        'posts_per_page' => -1
    ));
    return $children_query->have_posts();
}

This keeps your templates clean while maintaining a clear separation of concerns.

Our network