Using WordPress ‘clean_post_cache’ PHP action

The clean_post_cache WordPress PHP action fires immediately after a post’s cache is cleaned, allowing you to execute additional custom code.

Usage

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

function your_custom_function($post_id, $post) {
    // your custom code here
}

Parameters

  • $post_id (int) – The ID of the post for which the cache was cleaned.
  • $post (WP_Post) – The post object associated with the given post ID.

More information

See WordPress Developer Resources: clean_post_cache

Examples

This example updates related posts when the given post’s cache is cleaned.

add_action('clean_post_cache', 'update_related_posts', 10, 2);

function update_related_posts($post_id, $post) {
    // Update related posts here
}

Send a notification when a post’s cache is cleaned

This example sends a notification to the site admin when a post’s cache is cleaned.

add_action('clean_post_cache', 'send_notification', 10, 2);

function send_notification($post_id, $post) {
    // Send notification to admin here
}

Log cache cleaning events

This example logs cache cleaning events for each post in a custom log file.

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

function log_cache_cleaning($post_id, $post) {
    // Log cache cleaning event here
}

Refresh custom cache when a post’s cache is cleaned

This example refreshes a custom cache associated with the given post when its cache is cleaned.

add_action('clean_post_cache', 'refresh_custom_cache', 10, 2);

function refresh_custom_cache($post_id, $post) {
    // Refresh custom cache for the given post here
}

Perform a custom action when a specific post’s cache is cleaned

This example performs a custom action only when the cache for a specific post (with a known ID) is cleaned.

add_action('clean_post_cache', 'custom_action_for_specific_post', 10, 2);

function custom_action_for_specific_post($post_id, $post) {
    if ($post_id == 123) {
        // Perform custom action for the specific post here
    }
}