update_object_term_cache

Updates the cache for the given term object ID(s).

Description

(void|false) update_object_term_cache( (string|array) $object_ids, (array|string) $object_type ); 

Note: Due to performance concerns, great care should be taken to only update term caches when necessary. Processing time can increase exponentially depending on both the number of passed term IDs and the number of taxonomies those terms belong to.

Caches will only be updated for terms not already cached.

Returns (void|false)

False if all of the terms in `$object_ids` are already cached.

Parameters (2)

0. $object_ids (string|array)
Comma-separated list or array of term object IDs.
1. $object_type (array|string)
The taxonomy object type.

Usage

  1. if ( !function_exists( 'update_object_term_cache' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // Comma-separated list or array of term object IDs. 
  5. $object_ids = null; 
  6.  
  7. // The taxonomy object type. 
  8. $object_type = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = update_object_term_cache($object_ids, $object_type); 
  12.  

Defined (1)

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

/wp-includes/taxonomy.php  
  1. function update_object_term_cache($object_ids, $object_type) { 
  2. if ( empty($object_ids) ) 
  3. return; 
  4.  
  5. if ( !is_array($object_ids) ) 
  6. $object_ids = explode(', ', $object_ids); 
  7.  
  8. $object_ids = array_map('intval', $object_ids); 
  9.  
  10. $taxonomies = get_object_taxonomies($object_type); 
  11.  
  12. $ids = array(); 
  13. foreach ( (array) $object_ids as $id ) { 
  14. foreach ( $taxonomies as $taxonomy ) { 
  15. if ( false === wp_cache_get($id, "{$taxonomy}_relationships") ) { 
  16. $ids[] = $id; 
  17. break; 
  18.  
  19. if ( empty( $ids ) ) 
  20. return false; 
  21.  
  22. $terms = wp_get_object_terms( $ids, $taxonomies, array( 
  23. 'fields' => 'all_with_object_id',  
  24. 'orderby' => 'name',  
  25. 'update_term_meta_cache' => false,  
  26. ) ); 
  27.  
  28. $object_terms = array(); 
  29. foreach ( (array) $terms as $term ) { 
  30. $object_terms[ $term->object_id ][ $term->taxonomy ][] = $term->term_id; 
  31.  
  32. foreach ( $ids as $id ) { 
  33. foreach ( $taxonomies as $taxonomy ) { 
  34. if ( ! isset($object_terms[$id][$taxonomy]) ) { 
  35. if ( !isset($object_terms[$id]) ) 
  36. $object_terms[$id] = array(); 
  37. $object_terms[$id][$taxonomy] = array(); 
  38.  
  39. foreach ( $object_terms as $id => $value ) { 
  40. foreach ( $value as $taxonomy => $terms ) { 
  41. wp_cache_add( $id, $terms, "{$taxonomy}_relationships" );