wp_get_object_terms

Retrieves the terms associated with the given object(s), in the supplied taxonomies.

Description

(array|WP_Error) wp_get_object_terms( (int|array) $object_ids, (string|array) $taxonomies, (array) $args = array() ); 

Returns (array|WP_Error)

The requested term data or empty array if no terms found. WP_Error if any of the $taxonomies don't exist.

Parameters (3)

0. $object_ids (int|array)
The ID(s) of the object(s) to retrieve.
1. $taxonomies (string|array)
The taxonomies to retrieve terms from.
2. $args — Optional. (array) => array()
See WP_Term_Query::__construct() for supported arguments.

Usage

  1. if ( !function_exists( 'wp_get_object_terms' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // The ID(s) of the object(s) to retrieve. 
  5. $object_ids = null; 
  6.  
  7. // The taxonomies to retrieve terms from. 
  8. $taxonomies = null; 
  9.  
  10. // See WP_Term_Query::__construct() for supported arguments. 
  11. $args = array(); 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wp_get_object_terms($object_ids, $taxonomies, $args); 
  15.  

Defined (1)

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

/wp-includes/taxonomy.php  
  1. function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { 
  2. global $wpdb; 
  3.  
  4. if ( empty( $object_ids ) || empty( $taxonomies ) ) 
  5. return array(); 
  6.  
  7. if ( !is_array($taxonomies) ) 
  8. $taxonomies = array($taxonomies); 
  9.  
  10. foreach ( $taxonomies as $taxonomy ) { 
  11. if ( ! taxonomy_exists($taxonomy) ) 
  12. return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); 
  13.  
  14. if ( !is_array($object_ids) ) 
  15. $object_ids = array($object_ids); 
  16. $object_ids = array_map('intval', $object_ids); 
  17.  
  18. $args = wp_parse_args( $args ); 
  19.  
  20. $args['taxonomy'] = $taxonomies; 
  21. $args['object_ids'] = $object_ids; 
  22.  
  23. $terms = get_terms( $args ); 
  24.  
  25. /** 
  26. * Filters the terms for a given object or objects. 
  27. * @since 4.2.0 
  28. * @param array $terms An array of terms for the given object or objects. 
  29. * @param array $object_ids Array of object IDs for which `$terms` were retrieved. 
  30. * @param array $taxonomies Array of taxonomies from which `$terms` were retrieved. 
  31. * @param array $args An array of arguments for retrieving terms for the given 
  32. * object(s). See wp_get_object_terms() for details. 
  33. */ 
  34. $terms = apply_filters( 'get_object_terms', $terms, $object_ids, $taxonomies, $args ); 
  35.  
  36. $object_ids = implode( ', ', $object_ids ); 
  37. $taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'"; 
  38.  
  39. /** 
  40. * Filters the terms for a given object or objects. 
  41. * The `$taxonomies` parameter passed to this filter is formatted as a SQL fragment. The 
  42. * {@see 'get_object_terms'} filter is recommended as an alternative. 
  43. * @since 2.8.0 
  44. * @param array $terms An array of terms for the given object or objects. 
  45. * @param int|array $object_ids Object ID or array of IDs. 
  46. * @param string $taxonomies SQL-formatted (comma-separated and quoted) list of taxonomy names. 
  47. * @param array $args An array of arguments for retrieving terms for the given object(s). 
  48. * See wp_get_object_terms() for details. 
  49. */ 
  50. return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args );