How to easily get post ancestors

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);

Related posts:

  1. How to count your blogroll links
  2. Automatically add a search field to your navigation menu

Leave a Reply