wp_check_post_hierarchy_for_loops() - WordPress 函数参考中文文档

返回介绍

wp_check_post_hierarchy_for_loops()

发布于 2017-09-11 字数2054 浏览 941 评论 0

wp_check_post_hierarchy_for_loops( int $post_parent,  int $post_ID )

Check the given subset of the post hierarchy for hierarchy loops.


描述

Prevents loops from forming and breaks those that it finds. Attached to the ‘wp_insert_post_parent’ filter.


参数

$post_parent

(int)
(Required)
ID of the parent for the post we’re checking.

$post_ID

(int)
(Required)
ID of the post we’re checking.


返回值

(int) The new post_parent for the post, 0 otherwise.


源代码

File: wp-includes/post.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;
}

更新日志

Version 描述
3.1.0 Introduced.

相关函数

Uses

  • wp-includes/functions.php:
    wp_find_hierarchy_loop()
  • wp-includes/post.php:
    wp_update_post()

User Contributed Notes

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!