Site icon CodeRevolution Knowledge Base

How to easily get post ancestors – CodeRevolution

When using WordPress as a CMS or building a breadcrumb, you often need to get the post ancestors. Today let’s have a look at how you can easily get post ancestors of a specific post.

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!

Exit mobile version