Using WordPress ‘clean_object_term_cache()’ PHP function

The clean_object_term_cache() WordPress PHP function removes the taxonomy relationship to terms from the cache. This function will remove the entire taxonomy relationship containing term $object_id, but only if the term IDs exist within the taxonomy $object_type.

Usage

Here’s a basic example:

clean_object_term_cache( $object_ids, $object_type );

In this example, $object_ids could be an integer or an array of term object IDs and $object_type is the taxonomy object type.

Parameters

  • $object_ids (int|array – Required): Single or list of term object ID(s).
  • $object_type (array|string – Required): The taxonomy object type.

More information

See WordPress Developer Resources: clean_object_term_cache()

Examples

Cleaning cache for a single term object ID

In this example, we are cleaning the cache for the term with the ID of 45 from the ‘category’ taxonomy.

clean_object_term_cache( 45, 'category' );

Cleaning cache for multiple term object IDs

Here, we are cleaning the cache for the terms with the IDs of 12, 34, and 56 from the ‘post_tag’ taxonomy.

clean_object_term_cache( array(12, 34, 56), 'post_tag' );

Cleaning cache for a single term object ID within a custom taxonomy

This example cleans the cache for the term with the ID of 78 within the ‘my_custom_taxonomy’ taxonomy.

clean_object_term_cache( 78, 'my_custom_taxonomy' );

Cleaning cache for multiple term object IDs within a custom taxonomy

In this case, we are cleaning the cache for the terms with the IDs of 90, 123, and 456 within the ‘another_custom_taxonomy’ taxonomy.

clean_object_term_cache( array(90, 123, 456), 'another_custom_taxonomy' );

Cleaning cache for a term object ID within multiple taxonomies

This example shows how to clean the cache for the term with the ID of 789 within the ‘category’ and ‘post_tag’ taxonomies.

clean_object_term_cache( 789, array('category', 'post_tag') );

Each of these examples would remove the taxonomy relationship from the cache for the specified term IDs within their respective taxonomies.