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?

Andy did a tremendous job in developing a new website for our company from scratch. His vision in developing a “state of the art” website for a furniture retailer was key to making our website one of the top sites in our industry. He was tireless in working to develop new ideas and translate them into a working part of our site.

- Paul Sanford
COO, Roomful Express Furniture