Using WordPress ‘clean_post_cache()’ PHP function

The clean_post_cache() WordPress PHP function is used to clean or delete a post from the cache. It also cleans the term object cache associated with the post ID. This function won’t run if $_wp_suspend_cache_invalidation is not empty, which can be managed using wp_suspend_cache_invalidation().


Here’s a basic way to use the clean_post_cache() function.

clean_post_cache( $post_id );

In this example, $post_id is the ID of the post you want to remove from the cache.


  • $post (int|WP_Post – Required): The Post ID or post object that you want to remove from the cache.

More information

See WordPress Developer Resources: clean_post_cache

This function was implemented in WordPress 2.5.0 and there’s no deprecation as of the last update. You can find the source code in wp-includes/cache.php.


Cleaning a Single Post Cache

This code is used to clean the cache for a post with an ID of 123.

$post_id = 123;
clean_post_cache( $post_id );

Cleaning the Cache for the Current Post

In a loop, you can clean the cache for the current post like this:

global $post;
clean_post_cache( $post->ID );

Cleaning the Cache for a Custom Post

For a custom post type, you’d do something like this:

$post_id = get_the_ID();
if ( 'custom_post_type' == get_post_type( $post_id ) ) {
    clean_post_cache( $post_id );

Cleaning the Cache when a Post is Updated

If you want to clean the cache whenever any post is updated, you can use the save_post action hook:

add_action( 'save_post', 'clean_post_cache' );

Cleaning the Cache for Multiple Posts

If you have an array of post IDs and you want to clean the cache for all of them:

$post_ids = array( 123, 124, 125 );
foreach ( $post_ids as $post_id ) {
    clean_post_cache( $post_id );

In these examples, clean_post_cache() function is used to ensure that the cache for the specific posts are cleared, ensuring that the latest data is served on the next request.