wp_check_post_hierarchy_for_loops

The timeline below displays how wordpress function wp_check_post_hierarchy_for_loops has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.2

/**
 * Checks the given subset of the post hierarchy for hierarchy loops.
 *
 * Prevents loops from forming and breaks those that it finds. Attached
 * to the {@see 'wp_insert_post_parent'} filter.
 *
 * @since 3.1.0
 *
 * @see wp_find_hierarchy_loop()
 *
 * @param int $post_parent ID of the parent for the post we're checking.
 * @param int $post_id     ID of the post we're checking.
 * @return int The new post_parent for the post, 0 otherwise.
 */
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;
}

WordPress Version: 6.1

/**
 * Checks the given subset of the post hierarchy for hierarchy loops.
 *
 * Prevents loops from forming and breaks those that it finds. Attached
 * to the {@see 'wp_insert_post_parent'} filter.
 *
 * @since 3.1.0
 *
 * @see wp_find_hierarchy_loop()
 *
 * @param int $post_parent ID of the parent for the post we're checking.
 * @param int $post_ID     ID of the post we're checking.
 * @return int The new post_parent for the post, 0 otherwise.
 */
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;
}

WordPress Version: 5.6

/**
 * Check the given subset of the post hierarchy for hierarchy loops.
 *
 * Prevents loops from forming and breaks those that it finds. Attached
 * to the {@see 'wp_insert_post_parent'} filter.
 *
 * @since 3.1.0
 *
 * @see wp_find_hierarchy_loop()
 *
 * @param int $post_parent ID of the parent for the post we're checking.
 * @param int $post_ID     ID of the post we're checking.
 * @return int The new post_parent for the post, 0 otherwise.
 */
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;
}

WordPress Version: 5.4

/**
 * Check the given subset of the post hierarchy for hierarchy loops.
 *
 * Prevents loops from forming and breaks those that it finds. Attached
 * to the {@see 'wp_insert_post_parent'} filter.
 *
 * @since 3.1.0
 *
 * @see wp_find_hierarchy_loop()
 *
 * @param int $post_parent ID of the parent for the post we're checking.
 * @param int $post_ID     ID of the post we're checking.
 * @return int The new post_parent for the post, 0 otherwise.
 */
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.
    $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;
}

WordPress Version: 5.3

/**
 * Check the given subset of the post hierarchy for hierarchy loops.
 *
 * Prevents loops from forming and breaks those that it finds. Attached
 * to the {@see 'wp_insert_post_parent'} filter.
 *
 * @since 3.1.0
 *
 * @see wp_find_hierarchy_loop()
 *
 * @param int $post_parent ID of the parent for the post we're checking.
 * @param int $post_ID     ID of the post we're checking.
 * @return int The new post_parent for the post, 0 otherwise.
 */
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.
    $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;
}

WordPress Version: 5.1

/**
 * Check the given subset of the post hierarchy for hierarchy loops.
 *
 * Prevents loops from forming and breaks those that it finds. Attached
 * to the {@see 'wp_insert_post_parent'} filter.
 *
 * @since 3.1.0
 *
 * @see wp_find_hierarchy_loop()
 *
 * @param int $post_parent ID of the parent for the post we're checking.
 * @param int $post_ID     ID of the post we're checking.
 * @return int The new post_parent for the post, 0 otherwise.
 */
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;
}

WordPress Version: 4.6

/**
 * Check the given subset of the post hierarchy for hierarchy loops.
 *
 * Prevents loops from forming and breaks those that it finds. Attached
 * to the {@see 'wp_insert_post_parent'} filter.
 *
 * @since 3.1.0
 *
 * @see wp_find_hierarchy_loop()
 *
 * @param int $post_parent ID of the parent for the post we're checking.
 * @param int $post_ID     ID of the post we're checking.
 * @return int The new post_parent for the post, 0 otherwise.
 */
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;
}

WordPress Version: 4.0

/**
 * 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.
 *
 * @since 3.1.0
 *
 * @see wp_find_hierarchy_loop()
 *
 * @param int $post_parent ID of the parent for the post we're checking.
 * @param int $post_ID     ID of the post we're checking.
 * @return int The new post_parent for the post, 0 otherwise.
 */
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;
}

WordPress Version: 3.7

/**
 * Checks 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.
 *
 * @since 3.1.0
 * @uses wp_find_hierarchy_loop()
 *
 * @param int $post_parent ID of the parent for the post we're checking.
 * @param int $post_ID ID of the post we're checking.
 *
 * @return int The new post_parent for the post.
 */
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;
}