Using WordPress ‘clean_term_cache()’ PHP function

The clean_term_cache() WordPress PHP function is used to remove all of the term IDs from the cache.

Usage

clean_term_cache( $ids, $taxonomy = '', $clean_taxonomy = true );

In this example, we’re clearing the cache for term IDs 3, 7, and 9 in the ‘categories’ taxonomy.

clean_term_cache( array(3,7,9), 'category' );

Parameters

  • $ids (int|array) – Required. This is a single term ID or an array of term IDs that you want to remove from the cache.
  • $taxonomy (string) – Optional. This is the slug of the taxonomy. If left empty, the function will use the taxonomies of the passed term IDs. Default is an empty string.
  • $clean_taxonomy (bool) – Optional. If set to true, it cleans taxonomy wide caches. If false, it cleans just individual term object caches. Default is true.

More information

See WordPress Developer Resources: clean_term_cache()

This function was introduced in WordPress version 3.0.0.

Examples

Clearing Cache for a Single Term ID in a Specific Taxonomy

In this example, we’re clearing the cache for term ID 15 in the ‘category’ taxonomy.

clean_term_cache( 15, 'category' );

Clearing Cache for Multiple Term IDs in a Specific Taxonomy

In this case, we’re clearing the cache for term IDs 1, 2, and 3 in the ‘post_tag’ taxonomy.

clean_term_cache( array(1,2,3), 'post_tag' );

Clearing Cache for a Single Term ID in Any Taxonomy

If the taxonomy is not specified, the function will clear the cache for the term ID in any taxonomy where it is found.

clean_term_cache( 4 );

Clearing Cache for a Single Term ID, Not Taxonomy-Wide

If the last parameter is set to false, the function will only clear the individual term object caches, not the taxonomy-wide caches.

clean_term_cache( 5, 'category', false );

Clearing Cache for Multiple Term IDs, Not Taxonomy-Wide

Similarly, we can clear the individual term object caches for multiple term IDs, without affecting the taxonomy-wide caches.

clean_term_cache( array(6,7,8), 'post_tag', false );