get_object_term_cache

Retrieves the taxonomy relationship to the term object id.

Description

(bool|array) get_object_term_cache( (int) $id, (string) $taxonomy ); 

Upstream functions (like get_the_terms() and is_object_in_term()) are responsible for populating the object-term relationship cache. The current function only fetches relationship data that is already in the cache.

Returns (bool|array)

Array of `WP_Term` objects, if cached False if cache is empty for `$taxonomy` and `$id`.

Parameters (2)

0. $id (int)
Term object ID.
1. $taxonomy (string)
Taxonomy name.

Usage

  1. if ( !function_exists( 'get_object_term_cache' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // Term object ID. 
  5. $id = -1; 
  6.  
  7. // Taxonomy name. 
  8. $taxonomy = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_object_term_cache($id, $taxonomy); 
  12.  

Defined (1)

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

/wp-includes/taxonomy.php  
  1. function get_object_term_cache( $id, $taxonomy ) { 
  2. $_term_ids = wp_cache_get( $id, "{$taxonomy}_relationships" ); 
  3.  
  4. // We leave the priming of relationship caches to upstream functions. 
  5. if ( false === $_term_ids ) { 
  6. return false; 
  7.  
  8. // Backward compatibility for if a plugin is putting objects into the cache, rather than IDs. 
  9. $term_ids = array(); 
  10. foreach ( $_term_ids as $term_id ) { 
  11. if ( is_numeric( $term_id ) ) { 
  12. $term_ids[] = intval( $term_id ); 
  13. } elseif ( isset( $term_id->term_id ) ) { 
  14. $term_ids[] = intval( $term_id->term_id ); 
  15.  
  16. // Fill the term objects. 
  17. _prime_term_caches( $term_ids ); 
  18.  
  19. $terms = array(); 
  20. foreach ( $term_ids as $term_id ) { 
  21. $terms[] = wp_cache_get( $term_id, 'terms' ); 
  22.  
  23. return array_map( 'get_term', $terms );