wc_get_object_terms

Helper to get cached object terms and filter by field using wp_list_pluck().

Description

(array) wc_get_object_terms( (int) $object_id, (string) $taxonomy, (constant) $field = null, (null) $index_key = null ); 

Works as a cached alternative for wp_get_post_terms() and wp_get_object_terms().

Returns (array)

Parameters (4)

0. $object_id (int)
The object id.
1. $taxonomy (string)
Taxonomy slug.
2. $field — Optional. (constant) => null
Field name.
3. $index_key — Optional. (null) => null
Index key name.

Usage

  1. if ( !function_exists( 'wc_get_object_terms' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-term-functions.php'; 
  3.  
  4. // The object id. 
  5. $object_id = -1; 
  6.  
  7. // Taxonomy slug. 
  8. $taxonomy = ''; 
  9.  
  10. // Field name. 
  11. $field = null; 
  12.  
  13. // Index key name. 
  14. $index_key = null; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = wc_get_object_terms($object_id, $taxonomy, $field, $index_key); 
  18.  

Defined (1)

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

/includes/wc-term-functions.php  
  1. function wc_get_object_terms( $object_id, $taxonomy, $field = null, $index_key = null ) { 
  2. // Test if terms exists. get_the_terms() return false when it finds no terms. 
  3. $terms = get_the_terms( $object_id, $taxonomy ); 
  4.  
  5. if ( $terms && ! is_wp_error( $terms ) ) { 
  6. if ( ! is_null( $field ) ) { 
  7. $terms = wp_list_pluck( $terms, $field, $index_key ); 
  8. } else { 
  9. $terms = array(); 
  10.  
  11. return $terms;