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 is a true professional who is one of the most capable people I have ever worked with. His enthusiasm for his job is impressive and his ability to adapt and evolve to a new task is incredible. He is one of those rare people who you can tell where you want to get to and he can engineer the path there for you and give you extra choices along the way. His ability to learn new skills, willingness to continue to grow and expand is astounding. He’s also very skilled at what he does now and has a great eye for design and usability, blending form and function with class. And he’s fun to spend time with.

- Claire Devereux Thompson
Sterling Advertising