How to easily get post ancestors

In order to get the post ancestors (also called parents) simply use the get_post_ancestors() function. This function takes a single argument which can be either the post ID or post object, and return the ancestors IDs as an array.

$ancestors = get_post_ancestors($post);

Thanks to Coen Jacobs for this great piece of code!

Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!

How to easily get post ancestors

Leave a Reply

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