wc_reorder_terms

Move a term before the a given element of its hierarchy level.

Description

(int) wc_reorder_terms( (int) $the_term, (int) $next_id, (string) $taxonomy, (int) $index = 0, (null) $terms = null ); 

Returns (int)

Parameters (5)

0. $the_term (int)
The term.
1. $next_id (int)
The id of the next sibling element in save hierarchy level
2. $taxonomy (string)
The taxonomy.
3. $index — Optional. (int)
(default: 0)
4. $terms — Optional. (null) => null
(default: null)

Usage

  1. if ( !function_exists( 'wc_reorder_terms' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-term-functions.php'; 
  3.  
  4. // The term. 
  5. $the_term = -1; 
  6.  
  7. // the id of the next sibling element in save hierarchy level 
  8. $next_id = -1; 
  9.  
  10. // The taxonomy. 
  11. $taxonomy = ''; 
  12.  
  13. // (default: 0) 
  14. $index = -1; 
  15.  
  16. // (default: null) 
  17. $terms = null; 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = wc_reorder_terms($the_term, $next_id, $taxonomy, $index, $terms); 
  21.  

Defined (1)

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

/includes/wc-term-functions.php  
  1. function wc_reorder_terms( $the_term, $next_id, $taxonomy, $index = 0, $terms = null ) { 
  2. if ( ! $terms ) $terms = get_terms( $taxonomy, 'menu_order=ASC&hide_empty=0&parent=0' ); 
  3. if ( empty( $terms ) ) return $index; 
  4.  
  5. $id = $the_term->term_id; 
  6.  
  7. $term_in_level = false; // flag: is our term to order in this level of terms 
  8.  
  9. foreach ( $terms as $term ) { 
  10.  
  11. if ( $term->term_id == $id ) { // our term to order, we skip 
  12. $term_in_level = true; 
  13. continue; // our term to order, we skip 
  14. // the nextid of our term to order, lets move our term here 
  15. if ( null !== $next_id && $term->term_id == $next_id ) { 
  16. $index++; 
  17. $index = wc_set_term_order( $id, $index, $taxonomy, true ); 
  18.  
  19. // set order 
  20. $index++; 
  21. $index = wc_set_term_order( $term->term_id, $index, $taxonomy ); 
  22.  
  23. // if that term has children we walk through them 
  24. $children = get_terms( $taxonomy, "parent={$term->term_id}&menu_order=ASC&hide_empty=0" ); 
  25. if ( ! empty( $children ) ) { 
  26. $index = wc_reorder_terms( $the_term, $next_id, $taxonomy, $index, $children ); 
  27.  
  28. // no nextid meaning our term is in last position 
  29. if ( $term_in_level && null === $next_id ) { 
  30. $index = wc_set_term_order( $id, $index + 1, $taxonomy, true ); 
  31.  
  32. return $index;