функция
since 2.5.0
get_post_ancestors()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
get_post_ancestors( int|WP_Post $post ): int[]
Описание
Получает идентификаторы предков записи.
Параметры
$post
int|WP_Post
обязательный
Идентификатор записи или объект записи.
Возвращаемое значение
int[]
Исходный код
wp-includes/post.php:1159
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;
}
История изменений
| Версия | Описание |
|---|---|
| 2.5.0 | Introduced. |

