wp_check_post_hierarchy_for_loops

函式
wp_check_post_hierarchy_for_loops ( $post_parent, $post_ID )
引數
  • (int) $post_parent ID of the parent for the post we're checking.
    Required:
  • (int) $post_ID ID of the post we're checking.
    Required:
返回值
  • (int) The new post_parent for the post, 0 otherwise.
相關
  • wp_find_hierarchy_loop()
定義位置
相關方法
wp_check_term_hierarchy_for_loopswp_find_hierarchy_loopwp_check_post_lockwp_check_comment_floodwp_check_widget_editor_deps
引入
3.1.0
棄用
-

wp_check_post_hierarchy_for_loops: 這是一個檢查文章層次結構中的迴圈的函式。它可以用來防止當文章被組織成一個層次結構時可能出現的無限迴圈。

檢查文章層次結構的給定子集是否有層次結構的迴圈。

防止迴圈的形成,並打破那些它發現的迴圈。附在{@see ‘wp_insert_post_parent’}過濾器上。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >