Using WordPress ‘clean_object_term_cache’ PHP action

The clean_object_term_cache WordPress action fires after the object term cache has been cleaned.

Usage

add_action('clean_object_term_cache', 'your_custom_function', 10, 2);

function your_custom_function($object_ids, $object_type) {
  // your custom code here
}

Parameters

  • $object_ids: array – An array of object IDs.
  • $object_type: string – Object type.

More information

See WordPress Developer Resources: clean_object_term_cache

Examples

Log cache cleaning

Log the cleaning of object term cache.

add_action('clean_object_term_cache', 'log_cache_cleaning', 10, 2);

function log_cache_cleaning($object_ids, $object_type) {
  // log the cache cleaning
  error_log("Cache cleaned for object type: {$object_type} with IDs: " . implode(', ', $object_ids));
}

Custom cache cleaning

Clean a custom cache when object term cache is cleaned.

add_action('clean_object_term_cache', 'clean_custom_cache', 10, 2);

function clean_custom_cache($object_ids, $object_type) {
  // clean the custom cache for the given object type and IDs
  my_custom_cache_clean_function($object_ids, $object_type);
}

Notify admin

Notify admin via email when object term cache is cleaned.

add_action('clean_object_term_cache', 'notify_admin_cache_cleaned', 10, 2);

function notify_admin_cache_cleaned($object_ids, $object_type) {
  // send email notification to admin
  $to = get_option('admin_email');
  $subject = "Object Term Cache Cleaned: {$object_type}";
  $message = "Cache cleaned for object type: {$object_type} with IDs: " . implode(', ', $object_ids);

  wp_mail($to, $subject, $message);
}

Update cache cleaning count

Update a custom counter for cache cleaning events.

add_action('clean_object_term_cache', 'update_cache_cleaning_count', 10, 2);

function update_cache_cleaning_count($object_ids, $object_type) {
  // update custom counter for cache cleaning events
  $count = get_option('cache_cleaning_count', 0);
  update_option('cache_cleaning_count', $count + 1);
}

Trigger custom action

Trigger a custom action when object term cache is cleaned.

add_action('clean_object_term_cache', 'trigger_custom_action', 10, 2);

function trigger_custom_action($object_ids, $object_type) {
  // trigger custom action based on object type
  do_action("custom_action_{$object_type}", $object_ids);
}