wc_terms_clauses

Add term ordering to get_terms.

Description

(array) wc_terms_clauses( (array) $clauses, (array) $taxonomies, (array) $args ); 

It enables the support a menu_order parameter to get_terms for the product_cat taxonomy. By default it is ASC.. It accepts DESC too.

To disable it, set it ot false (or 0).

Returns (array)

Parameters (3)

0. $clauses (array)
The clauses.
1. $taxonomies (array)
The taxonomies.
2. $args (array)
The args.

Usage

  1. if ( !function_exists( 'wc_terms_clauses' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-term-functions.php'; 
  3.  
  4. // The clauses. 
  5. $clauses = array(); 
  6.  
  7. // The taxonomies. 
  8. $taxonomies = array(); 
  9.  
  10. // The args. 
  11. $args = array(); 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wc_terms_clauses($clauses, $taxonomies, $args); 
  15.  

Defined (1)

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

/includes/wc-term-functions.php  
  1. function wc_terms_clauses( $clauses, $taxonomies, $args ) { 
  2. global $wpdb; 
  3.  
  4. // No sorting when menu_order is false. 
  5. if ( isset( $args['menu_order'] ) && ( false === $args['menu_order'] || 'false' === $args['menu_order'] ) ) { 
  6. return $clauses; 
  7.  
  8. // No sorting when orderby is non default. 
  9. if ( isset( $args['orderby'] ) && 'name' !== $args['orderby'] ) { 
  10. return $clauses; 
  11.  
  12. // No sorting in admin when sorting by a column. 
  13. if ( is_admin() && isset( $_GET['orderby'] ) ) { 
  14. return $clauses; 
  15.  
  16. // No need to filter counts 
  17. if ( strpos( 'COUNT(*)', $clauses['fields'] ) !== false ) { 
  18. return $clauses; 
  19.  
  20. // WordPress should give us the taxonomies asked when calling the get_terms function. Only apply to categories and pa_ attributes. 
  21. $found = false; 
  22. foreach ( (array) $taxonomies as $taxonomy ) { 
  23. if ( taxonomy_is_product_attribute( $taxonomy ) || in_array( $taxonomy, apply_filters( 'woocommerce_sortable_taxonomies', array( 'product_cat' ) ) ) ) { 
  24. $found = true; 
  25. break; 
  26. if ( ! $found ) { 
  27. return $clauses; 
  28.  
  29. // Meta name. 
  30. if ( ! empty( $taxonomies[0] ) && taxonomy_is_product_attribute( $taxonomies[0] ) ) { 
  31. $meta_name = 'order_' . esc_attr( $taxonomies[0] ); 
  32. } else { 
  33. $meta_name = 'order'; 
  34.  
  35. // Query fields. 
  36. $clauses['fields'] = 'DISTINCT ' . $clauses['fields'] . ', tm.meta_value'; 
  37.  
  38. // Query join. 
  39. if ( get_option( 'db_version' ) < 34370 ) { 
  40. $clauses['join'] .= " LEFT JOIN {$wpdb->woocommerce_termmeta} AS tm ON (t.term_id = tm.woocommerce_term_id AND tm.meta_key = '" . esc_sql( $meta_name ) . "') "; 
  41. } else { 
  42. $clauses['join'] .= " LEFT JOIN {$wpdb->termmeta} AS tm ON (t.term_id = tm.term_id AND tm.meta_key = '" . esc_sql( $meta_name ) . "') "; 
  43.  
  44. // Default to ASC. 
  45. if ( ! isset( $args['menu_order'] ) || ! in_array( strtoupper( $args['menu_order'] ), array( 'ASC', 'DESC' ) ) ) { 
  46. $args['menu_order'] = 'ASC'; 
  47.  
  48. $order = "ORDER BY tm.meta_value+0 " . $args['menu_order']; 
  49.  
  50. if ( $clauses['orderby'] ) { 
  51. $clauses['orderby'] = str_replace( 'ORDER BY', $order . ', ', $clauses['orderby'] ); 
  52. } else { 
  53. $clauses['orderby'] = $order; 
  54.  
  55. return $clauses;