clean_term_cache

Will remove all of the term ids from the cache.

Description

clean_term_cache( (int|array) $ids, (string) $taxonomy = '', (constant) $clean_taxonomy = true ); 

Parameters (3)

0. $ids (int|array)
Single or list of Term IDs.
1. $taxonomy — Optional. (string) => ''
Can be empty and will assume tt_ids,, else will use for context. Default empty.
2. $clean_taxonomy — Optional. (constant) => true
Whether to clean taxonomy wide caches (true), or just individual term object caches (false). Default true.

Usage

  1. if ( !function_exists( 'clean_term_cache' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // Single or list of Term IDs. 
  5. $ids = null; 
  6.  
  7. // Optional. Can be empty and will assume `tt_ids`, else will use for context. 
  8. // Default empty. 
  9. $taxonomy = ''; 
  10.  
  11. // Optional. Whether to clean taxonomy wide caches (true), or just individual 
  12. // term object caches (false). Default true. 
  13. $clean_taxonomy = true; 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = clean_term_cache($ids, $taxonomy, $clean_taxonomy); 
  17.  

Defined (1)

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

/wp-includes/taxonomy.php  
  1. function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) { 
  2. global $wpdb, $_wp_suspend_cache_invalidation; 
  3.  
  4. if ( ! empty( $_wp_suspend_cache_invalidation ) ) { 
  5. return; 
  6.  
  7. if ( !is_array($ids) ) 
  8. $ids = array($ids); 
  9.  
  10. $taxonomies = array(); 
  11. // If no taxonomy, assume tt_ids. 
  12. if ( empty($taxonomy) ) { 
  13. $tt_ids = array_map('intval', $ids); 
  14. $tt_ids = implode(', ', $tt_ids); 
  15. $terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)"); 
  16. $ids = array(); 
  17. foreach ( (array) $terms as $term ) { 
  18. $taxonomies[] = $term->taxonomy; 
  19. $ids[] = $term->term_id; 
  20. wp_cache_delete( $term->term_id, 'terms' ); 
  21. $taxonomies = array_unique($taxonomies); 
  22. } else { 
  23. $taxonomies = array($taxonomy); 
  24. foreach ( $taxonomies as $taxonomy ) { 
  25. foreach ( $ids as $id ) { 
  26. wp_cache_delete( $id, 'terms' ); 
  27.  
  28. foreach ( $taxonomies as $taxonomy ) { 
  29. if ( $clean_taxonomy ) { 
  30. wp_cache_delete('all_ids', $taxonomy); 
  31. wp_cache_delete('get', $taxonomy); 
  32. delete_option("{$taxonomy}_children"); 
  33. // Regenerate {$taxonomy}_children 
  34. _get_term_hierarchy($taxonomy); 
  35.  
  36. /** 
  37. * Fires once after each taxonomy's term cache has been cleaned. 
  38. * @since 2.5.0 
  39. * @since 4.5.0 Added the `$clean_taxonomy` parameter. 
  40. * @param array $ids An array of term IDs. 
  41. * @param string $taxonomy Taxonomy slug. 
  42. * @param bool $clean_taxonomy Whether or not to clean taxonomy-wide caches 
  43. */ 
  44. do_action( 'clean_term_cache', $ids, $taxonomy, $clean_taxonomy ); 
  45.  
  46. wp_cache_set( 'last_changed', microtime(), 'terms' );