Is current WordPress page in the hierarchy of some other?

Featured Image

This topic could be interested for those of you who deal with WordPress theme creations. If you are not one of them, you will not find this interesting at all. Developing a complex WordPress site with many sections with pages may be limited with WordPress standard content structure. Posts are in categories. Categories can have subcategories and pages can have subpages. There are some default functions that help you in identification of which element is child and which is parent. But when we need something more advanced, things get complicated.

I needed a function that identifies if current page is in a page hierarchy of some other.
So, let’s say if we have certain page structure:
– Main Page #1 (ID: 1)
— Sub page #1 (ID: 2)
— Sub page #2 (ID: 2)
— This Page is a Sub Page of #2 (ID: 4)
– Main Page #2 (ID: 5)

If we are currently on Page with ID:4 and run the function call: in_page_hierarchy_for(1), the function will return true. If we run in_page_hierarchy_for(5), the function will return false.

* @desc Checks if current page is page hierarchy of some other page
* @param mixed $page_id Page ID or array of Page IDs
* @returns bool
function in_page_hierarchy_for($page_id) {
	if (is_array($page_id)) {
		$r = false;
		foreach ($page_id as $pid) {
			if (in_page_hierarchy_for($pid) == true) $r = true;
		return $r;
	} else {
		global $post;
		if (is_page($page_id) or ($post->post_type == 'page' and isset($post->ancestors) && in_array($page_id, $post->ancestors))) return true;
		else return false;

About Tomislav Bilic

Founder and CEO

Tomislav is a founder and CEO at Inchoo. Enjoys traveling - especially quick getaways, traditional cuisine (from most cultures), good wine and strong rakija.

Read more posts by Tomislav / Visit Tomislav's profile


  1. hi,
    that seems exactly what i’m looking for. though i’m not familar with php, i’m wondering how to include it into my code.
    i want to change an image according to the ancestor page
    for the example above: image#1 for all childpages (and grandchildpages etc.) of Main Page#1, and show image#2 to for all childpages (and grandchildpages etc.)of Main Page#2 and so on
    may anybody help me?

  2. This looks useful for complicated hierarchies. (Does anyone do that?)
    I have a couple of problems with the code though.

    First, always use a single return! especially with a recursive function.

    Second, if you pass in an array you’re asking the question “Is the current page in any of these hierarchies?”, so you can fully return as soon as you find a yes answer. To do that, put a break in the if of the foreach.

    Third, the ‘and’ and ‘or’ operators have lower precedence, so your if statement is a little funky. This functions should only be checking actual pages, not posts, so the check for $post->post_type==’page’ should be before anything else is done, around line 2. Because the ‘==’ is highest precedence of the expression, it gets evaluated first, then the isset and the in_array, then the ‘&&’ of the two, then the ‘and’ with the first part. If none of it was true, it will finally check if the page_id is a page. This leads me to question why you have this check returning true like that…if the current post is not a page, but the id you are checking is, you will wrongly return a true value.

    I’m putting a modified version here, please pardon me in advance if it doesn’t come through correctly from this comment form. I didn’t test the changes I made.

    function in_page_hierarchy_for($page_id) {
    global $post;
    $r = false;
    if ($post->post_type == 'page' && isset($post->ancestors)) {
    if (is_array($page_id)) {
    foreach ($page_id as $pid)
    if (in_page_hierarchy_for($pid) == true) {
    $r = true;
    else {
    if (is_page($page_id) && in_array($page_id, $post->ancestors))
    $r = true;
    return $r;

  3. Perfect! This is exactly what I needed today. Thank you so much, saved me some time hacking my own php.

    By the way, on line 15, the html entities for the ampersands need to be replaced with the actual things.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.