Uses
Uses | Description |
---|---|
wp-includes/post.php: get_post() | Retrieves post data given a post ID or post object. |
Retrieve ancestors of a post.
(int|WP_Post) (Required) Post ID or post object.
(int[]) Ancestor IDs or empty array if none are found.
File: wp-includes/post.php
function get_post_ancestors( $post ) { $post = get_post( $post ); if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID ) { return array(); } $ancestors = array(); $id = $post->post_parent; $ancestors[] = $id; 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, true ) ) { break; } $id = $ancestor->post_parent; $ancestors[] = $id; } return $ancestors; }
Version | Description |
---|---|
2.5.0 | Introduced. |
© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_post_ancestors