Using WordPress ‘clean_category_cache()’ PHP function

The clean_category_cache() WordPress PHP function is used to remove the category cache data based on a specific ID.

Usage

To use the clean_category_cache() function, you simply need to pass in the ID of the category you want to clear from the cache.

clean_category_cache(15);

In this example, the cache for the category with the ID of 15 is cleared.

Parameters

  • $id (int) – The ID of the category you wish to clear from the cache.

More information

See WordPress Developer Resources: clean_category_cache

This function was implemented in WordPress 3.1.0. It has not been deprecated as of the latest WordPress version. The source code for this function is located in wp-includes/category.php.

Examples

Clearing a Category Cache

This example clears the cache for category 12.

clean_category_cache(12);

Clearing Multiple Category Caches

This example demonstrates how to clear the cache for multiple categories using an array of IDs.

$categories = array(1, 2, 3, 4, 5);

foreach ($categories as $category_id) {
    clean_category_cache($category_id);
}

Clearing Cache after Creating a Category

This example creates a new category and then immediately clears the cache.

$category_id = wp_create_category('New Category');
clean_category_cache($category_id);

Clearing Cache after Updating a Category

This example shows how to clear the cache after updating a category.

wp_update_category(array('cat_ID' => 2, 'category_nicename' => 'updated-category'));
clean_category_cache(2);

Clearing Cache after Deleting a Category

This example shows how to clear the cache after deleting a category.

wp_delete_category(3);
clean_category_cache(3);

In each of these examples, the clean_category_cache() function is used to clear the cache for specific category IDs. This helps to ensure that the most up-to-date category data is used in your WordPress site.