Title: wp_find_hierarchy_loop_tortoise_hare
Published: April 25, 2014
Last modified: August 20, 2026

---

# wp_find_hierarchy_loop_tortoise_hare( callable $callback, int $start, array $override = array(), array $callback_args = array(), bool $_return_loop = false ): mixed

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop_tortoise_hare/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop_tortoise_hare/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop_tortoise_hare/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop_tortoise_hare/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop_tortoise_hare/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop_tortoise_hare/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop_tortoise_hare/?output_format=md#wp--skip-link--target)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Uses the “The Tortoise and the Hare” algorithm to detect loops.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop_tortoise_hare/?output_format=md#description)󠁿

For every step of the algorithm, the hare takes two steps and the tortoise one.

If the hare ever laps the tortoise, there must be a loop.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop_tortoise_hare/?output_format=md#parameters)󠁿

 `$callback`callablerequired

Function that accepts ( ID, callback_arg, … ) and outputs parent_ID.

`$start`intrequired

The ID to start the loop check at.

`$override`arrayoptional

An array of ( ID => parent_ID, … ) to use instead of $callback.

Default:`array()`

`$callback_args`arrayoptional

Additional arguments to send to $callback.

Default:`array()`

`$_return_loop`booloptional

Return loop members or just detect presence of loop? Only set to true if you already
know the given $start is part of a loop (otherwise the returned array might include
branches).

Default:`false`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop_tortoise_hare/?output_format=md#return)󠁿

 mixed Scalar ID of some arbitrary member of the loop, or array of IDs of all members
of loop if $_return_loop

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop_tortoise_hare/?output_format=md#source)󠁿

    ```php
    function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) {
    	$tortoise        = $start;
    	$hare            = $start;
    	$evanescent_hare = $start;
    	$return          = array();

    	// Set evanescent_hare to one past hare. Increment hare two steps.
    	while (
    		$tortoise
    	&&
    		( $evanescent_hare = $override[ $hare ] ?? call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) )
    	&&
    		( $hare = $override[ $evanescent_hare ] ?? call_user_func_array( $callback, array_merge( array( $evanescent_hare ), $callback_args ) ) )
    	) {
    		if ( $_return_loop ) {
    			$return[ $tortoise ]        = true;
    			$return[ $evanescent_hare ] = true;
    			$return[ $hare ]            = true;
    		}

    		// Tortoise got lapped - must be a loop.
    		if ( $tortoise === $evanescent_hare || $tortoise === $hare ) {
    			return $_return_loop ? $return : $tortoise;
    		}

    		// Increment tortoise by one step.
    		$tortoise = $override[ $tortoise ] ?? call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) );
    	}

    	return false;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/functions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/functions.php#L7242)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/functions.php#L7242-L7272)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop_tortoise_hare/?output_format=md#related)󠁿

| Used by | Description | 
| [wp_find_hierarchy_loop()](https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop/)`wp-includes/functions.php` |

Finds hierarchy loops using a callback function that maps object IDs to parent IDs.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop_tortoise_hare/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.1.0](https://developer.wordpress.org/reference/since/3.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_find_hierarchy_loop_tortoise_hare%2F)
before being able to contribute a note or feedback.