wp_check_term_hierarchy_for_loops

Checks the given subset of the term hierarchy for hierarchy loops.

Description

(int) wp_check_term_hierarchy_for_loops( (int) $parent, (int) $term_id, (string) $taxonomy ); 

Prevents loops from forming and breaks those that it finds.

Attached to the filter.

Returns (int)

The new parent for the term.

Parameters (3)

0. $parent (int)
'term_id' of the parent for the term we're checking.
1. $term_id (int)
The term we're checking.
2. $taxonomy (string)
The taxonomy of the term we're checking.

Usage

  1. if ( !function_exists( 'wp_check_term_hierarchy_for_loops' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // `term_id` of the parent for the term we're checking. 
  5. $parent = -1; 
  6.  
  7. // The term we're checking. 
  8. $term_id = -1; 
  9.  
  10. // The taxonomy of the term we're checking. 
  11. $taxonomy = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wp_check_term_hierarchy_for_loops($parent, $term_id, $taxonomy); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/taxonomy.php  
  1. function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) { 
  2. // Nothing fancy here - bail 
  3. if ( !$parent ) 
  4. return 0; 
  5.  
  6. // Can't be its own parent. 
  7. if ( $parent == $term_id ) 
  8. return 0; 
  9.  
  10. // Now look for larger loops. 
  11. if ( !$loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) ) 
  12. return $parent; // No loop 
  13.  
  14. // Setting $parent to the given value causes a loop. 
  15. if ( isset( $loop[$term_id] ) ) 
  16. return 0; 
  17.  
  18. // There's a loop, but it doesn't contain $term_id. Break the loop. 
  19. foreach ( array_keys( $loop ) as $loop_member ) 
  20. wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) ); 
  21.  
  22. return $parent;