检索帖子的祖先。

原型

get_post_ancestors( int|WP_Post $post )

参数

$post (int|WP_Post) (Required) 帖子ID或帖子对象。

返回值

(array)  祖先ID或空数组(如果没有找到)。

源文件

路径:wp-includes/post.php

<?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 = $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;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/get_post_ancestors/