wp_check_post_hierarchy_for_loops() WordPress Function

The wp_check_post_hierarchy_for_loops() function is used to check if a post is in a valid hierarchy for the loop. This function is used by the WordPress loop to ensure that the correct posts are displayed.

wp_check_post_hierarchy_for_loops( int $post_parent, int $post_ID ) #

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


Description

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

Top ↑

See also


Top ↑

Parameters

$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.


Top ↑

Return

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


Top ↑

Source

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 ( ! $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;
}


Top ↑

Changelog

Changelog
VersionDescription
3.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More