Retrieve ancestors of a post.
Signature
get_post_ancestors( $post )
- post
- (int|object) Post ID or post object
Return
(array) Ancestor IDs or empty array if none are found.
Source
function get_post_ancestors( $post ) {
if ( ! $post )
return array();
$post = get_post( $post );
if ( empty( $post->post_parent ) || $post->post_parent == $post->ID )
return array();
$ancestors = array();
$id = $ancestors[] = $post->post_parent;
while ( $ancestor = get_post( $id ) ) {
// Loop detection: If the ancestor has been seen before, break.
if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) )
break;
$id = $ancestors[] = $ancestor->post_parent;
}
return $ancestors;
}
WP Trac GitHub Bitbucket
Link here
-
URL
http://queryposts.com/function/get_post_ancestors/ -
HTML
<a href='http://queryposts.com/function/get_post_ancestors/'>get_post_ancestors()</a> -
Markdown
[get_post_ancestors()](http://queryposts.com/function/get_post_ancestors/) -
BBCode
[url=http://queryposts.com/function/get_post_ancestors/]get_post_ancestors()[/url]