检查层次结构循环的后层次结构的给定子集。
原型
wp_check_post_hierarchy_for_loops( int $post_parent, int $post_ID )
描述
防止循环形成并破坏它找到的循环。附加到’wp_insert_post_parent’过滤器。
参考:
- wp_find_hierarchy_loop()
参数
$post_parent
(int)
(Required)
我们正在检查的帖子的父级ID。
$post_ID
(int)
(Required)
我们正在检查的帖子的ID。
返回值
(int)
帖子的新post_parent,否则为0。
源文件
路径:wp-includes/post.php
<?php
...
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 ( empty( $post_ID ) )
return $post_parent;
// Can't be its own parent.
if ( $post_parent == $post_ID )
return 0;
// Now look for larger loops.
if ( !$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) )
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;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/wp_check_post_hierarchy_for_loops/