Using WordPress ‘clean_taxonomy_cache()’ PHP function

The clean_taxonomy_cache() WordPress PHP function is used to clean the caches for a specific taxonomy.

Usage

To use the clean_taxonomy_cache() function, you simply need to pass in the slug of the taxonomy you want to clean the cache for.

clean_taxonomy_cache( 'category' );

In the above example, we’re clearing the cache for the ‘category’ taxonomy.

Parameters

  • $taxonomy (string) – Required. The slug of the taxonomy for which you want to clean the cache.

More information

See WordPress Developer Resources: clean_taxonomy_cache()

This function was implemented in WordPress 4.9.0. Please refer to the WordPress Developer Resources for any updates on depreciation.

Examples

Clearing Cache for Custom Taxonomy

If you have a custom taxonomy, let’s say ‘book_genre’, you can clear the cache for it as follows:

// Clear the cache for the 'book_genre' taxonomy
clean_taxonomy_cache( 'book_genre' );

Clearing Cache for Multiple Taxonomies

If you want to clear the cache for multiple taxonomies, you can do so by calling the function multiple times:

// Clear the cache for 'category' and 'post_tag' taxonomies
clean_taxonomy_cache( 'category' );
clean_taxonomy_cache( 'post_tag' );

Clearing Cache after Adding a New Term

You might want to clear the cache after adding a new term to a taxonomy:

// Add a new term to 'category'
wp_insert_term( 'New Category', 'category' );

// Clear the cache for 'category'
clean_taxonomy_cache( 'category' );

Clearing Cache after Deleting a Term

Similarly, after deleting a term from a taxonomy, it would be wise to clear the cache:

// Delete a term from 'category'
wp_delete_term( 123, 'category' );

// Clear the cache for 'category'
clean_taxonomy_cache( 'category' );

Clearing Cache after Editing a Term

Finally, you should clear the cache after editing a term in a taxonomy:

// Edit a term in 'category'
wp_update_term( 123, 'category', array( 'name' => 'Updated Category' ) );

// Clear the cache for 'category'
clean_taxonomy_cache( 'category' );