wp_check_post_hierarchy_for_loops()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
wp_check_post_hierarchy_for_loops( int $post_parent, int $post_id ): int
Описание
Предотвращает образование циклов и разрывает те, что находит. Подключена к фильтру 'wp_insert_post_parent'.
См. alsowp_find_hierarchy_loop()
Оригинал (английский)
Prevents loops from forming and breaks those that it finds. Attached to the ‘wp_insert_post_parent’ filter.
Параметры
$post_parent
int
обязательный
$post_id
int
обязательный
Возвращаемое значение
int
Исходный код
wp-includes/post.php:8062
function wp_check_post_hierarchy_for_loops( $post_parent, $post_id ) {
// Nothing fancy here - bail.
if ( ! $post_parent ) {
return 0;
}
// New post can't cause a loop.
if ( ! $post_id ) {
return $post_parent;
}
// Can't be its own parent.
if ( $post_parent === $post_id ) {
return 0;
}
// Now look for larger loops.
$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_id, $post_parent );
if ( ! $loop ) {
return $post_parent; // No loop.
}
// Setting $post_parent to the given value causes a loop.
if ( isset( $loop[ $post_id ] ) ) {
return 0;
}
// There's a loop, but it doesn't contain $post_id. Break the loop.
foreach ( array_keys( $loop ) as $loop_member ) {
wp_update_post(
array(
'ID' => $loop_member,
'post_parent' => 0,
)
);
}
return $post_parent;
}
История изменений
| Версия | Описание |
|---|---|
| 3.1.0 | Introduced. |

