wc_set_term_order

Set the sort order of a term.

Description

(int) wc_set_term_order( (int) $term_id, (int) $index, (string) $taxonomy, (bool) $recursive = false ); 

Returns (int)

Parameters (4)

0. $term_id (int)
The term id.
1. $index (int)
The index.
2. $taxonomy (string)
The taxonomy.
3. $recursive — Optional. (bool) => false
(default: false)

Usage

  1. if ( !function_exists( 'wc_set_term_order' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-term-functions.php'; 
  3.  
  4. // The term id. 
  5. $term_id = -1; 
  6.  
  7. // The index. 
  8. $index = -1; 
  9.  
  10. // The taxonomy. 
  11. $taxonomy = ''; 
  12.  
  13. // (default: false) 
  14. $recursive = false; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = wc_set_term_order($term_id, $index, $taxonomy, $recursive); 
  18.  

Defined (1)

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

/includes/wc-term-functions.php  
  1. function wc_set_term_order( $term_id, $index, $taxonomy, $recursive = false ) { 
  2.  
  3. $term_id = (int) $term_id; 
  4. $index = (int) $index; 
  5.  
  6. // Meta name 
  7. if ( taxonomy_is_product_attribute( $taxonomy ) ) 
  8. $meta_name = 'order_' . esc_attr( $taxonomy ); 
  9. else 
  10. $meta_name = 'order'; 
  11.  
  12. update_woocommerce_term_meta( $term_id, $meta_name, $index ); 
  13.  
  14. if ( ! $recursive ) return $index; 
  15.  
  16. $children = get_terms( $taxonomy, "parent=$term_id&menu_order=ASC&hide_empty=0" ); 
  17.  
  18. foreach ( $children as $term ) { 
  19. $index++; 
  20. $index = wc_set_term_order( $term->term_id, $index, $taxonomy, true ); 
  21.  
  22. clean_term_cache( $term_id, $taxonomy ); 
  23.  
  24. return $index;