Great Grand Parent ID’s in WordPress

Wordpress How-To's

Have you ever needed to find all the ancestor ID’s of a page in WordPress? If so, you’ve come to the right place. Here’s some quick code to give you up to the great grand parent ID.

{code type=php} $current = $post->ID;
$parent = $post->post_parent;
$get_grandparent = get_post($parent);
$grandparent = $get_grandparent->post_parent;
$get_greatgrandparent = get_post($grandparent);
$greatgrandparent = $get_greatgrandparent->post_parent;

echo “parent = $parent; grandparent = $grandparent; greatgrandparent = $greatgrandparent”;

{/code} If you wanted to, you could continue this pattern to N number of ancestors…

Hopefully you’ve found this quick tutorial valuable. Contact me if you have any questions or would do it a different way. There’s 1,001 ways to do everything – that’s why I love coding.

Leave a Reply

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

What's the Buzz?

In working with Andy, I was extremely impressed by his overall expertise and the “can-do” attitude with which he approached every phase of the project. While some designers may have become frustrated by working with multiple decision makers with limited web development knowledge, Andy was very understanding and strategic in his approach to finding very organized solutions to address our needs. I would highly recommend him!

- Allison Davies
Ten United