wc_get_product_terms

Wrapper for wp_get_post_terms which supports ordering by parent.

Description

(array) wc_get_product_terms( (int) $product_id, (string) $taxonomy, (array) $args = array() ); 

NOTE: At this point in time, ordering by menu_order for example isn't possible with this function. wp_get_post_terms has no. filters which we can utilise to modify it's query. https://core.trac.wordpress.org/ticket/19094.

Returns (array)

Parameters (3)

0. $product_id (int)
The product id.
1. $taxonomy (string)
Taxonomy slug.
2. $args — Optional. (array) => array()
Query arguments.

Usage

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

Defined (1)

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

/includes/wc-term-functions.php  
  1. function wc_get_product_terms( $product_id, $taxonomy, $args = array() ) { 
  2. if ( ! taxonomy_exists( $taxonomy ) ) { 
  3. return array(); 
  4.  
  5. if ( empty( $args['orderby'] ) && taxonomy_is_product_attribute( $taxonomy ) ) { 
  6. $args['orderby'] = wc_attribute_orderby( $taxonomy ); 
  7.  
  8. // Support ordering by parent. 
  9. if ( ! empty( $args['orderby'] ) && in_array( $args['orderby'], array( 'name_num', 'parent' ) ) ) { 
  10. $fields = isset( $args['fields'] ) ? $args['fields'] : 'all'; 
  11. $orderby = $args['orderby']; 
  12.  
  13. // Unset for wp_get_post_terms. 
  14. unset( $args['orderby'] ); 
  15. unset( $args['fields'] ); 
  16.  
  17. $terms = _wc_get_cached_product_terms( $product_id, $taxonomy, $args ); 
  18.  
  19. switch ( $orderby ) { 
  20. case 'name_num' : 
  21. break; 
  22. case 'parent' : 
  23. break; 
  24.  
  25. switch ( $fields ) { 
  26. case 'names' : 
  27. $terms = wp_list_pluck( $terms, 'name' ); 
  28. break; 
  29. case 'ids' : 
  30. $terms = wp_list_pluck( $terms, 'term_id' ); 
  31. break; 
  32. case 'slugs' : 
  33. $terms = wp_list_pluck( $terms, 'slug' ); 
  34. break; 
  35. } elseif ( ! empty( $args['orderby'] ) && 'menu_order' === $args['orderby'] ) { 
  36. // wp_get_post_terms doesn't let us use custom sort order. 
  37. $args['include'] = wc_get_object_terms( $product_id, $taxonomy, 'term_id' ); 
  38.  
  39. if ( empty( $args['include'] ) ) { 
  40. $terms = array(); 
  41. } else { 
  42. // This isn't needed for get_terms. 
  43. unset( $args['orderby'] ); 
  44.  
  45. // Set args for get_terms. 
  46. $args['menu_order'] = isset( $args['order'] ) ? $args['order'] : 'ASC'; 
  47. $args['hide_empty'] = isset( $args['hide_empty'] ) ? $args['hide_empty'] : 0; 
  48. $args['fields'] = isset( $args['fields'] ) ? $args['fields'] : 'names'; 
  49.  
  50. // Ensure slugs is valid for get_terms - slugs isn't supported. 
  51. $args['fields'] = ( 'slugs' === $args['fields'] ) ? 'id=>slug' : $args['fields']; 
  52. $terms = get_terms( $taxonomy, $args ); 
  53. } else { 
  54. $terms = _wc_get_cached_product_terms( $product_id, $taxonomy, $args ); 
  55.  
  56. return apply_filters( 'woocommerce_get_product_terms' , $terms, $product_id, $taxonomy, $args );