Great Grand Parent ID’s in WordPress


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.

 $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";

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.

Share and Enjoy:
  • Print
  • Digg
  • Facebook
  • Sphinn
  • Google Bookmarks
  • del.icio.us
  • Mixx
  • StumbleUpon
  • email

Posted on July 10, 2009 in Wordpress How-To's and has been tagged as .


Feedback for "Great Grand Parent ID’s in WordPress"

9 Comments

Categories

Services I Offer

About Me

Andy Weigel

My name is Andy Weigel and I'm a web developer and designer in Pittsburgh, PA.

I focus on combining design with technology to build compelling, creative, easy-to-navigate web sites and custom web applications for organizations and businesses of all shapes and sizes. My specialty is WordPress. And most of all, I love what I do!