clean_post_cache()WP 2.0.0

Deletes the object cache of the post by the given ID. The cache of terms and child posts will also be deleted.

The function calls itself recursively when deleting the cache of child posts.

Will not work if $_wp_suspend_cache_invalidation is not empty. See: wp_suspend_cache_invalidation().

Comment cache can be cleared using the function clean_comment_cache().

Hooks from the function

Returns

null. Does not return anything.

Usage

clean_post_cache( $id );
$id(integer/WP_Post) (required)
ID or post object whose cache needs to be cleared.

Examples

0

#1 Delete post object cache

Let's say we're editing post 25 and we need to clear it's object cache:

$id = 25;
clean_post_cache( $id );

Notes

  • Global. true|false. $_wp_suspend_cache_invalidation

Changelog

Since 2.0.0 Introduced.

clean_post_cache() code WP 6.9.1

function clean_post_cache( $post ) {
	global $_wp_suspend_cache_invalidation;

	if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
		return;
	}

	$post = get_post( $post );

	if ( ! $post ) {
		return;
	}

	wp_cache_delete( $post->ID, 'posts' );
	wp_cache_delete( 'post_parent:' . (string) $post->ID, 'posts' );
	wp_cache_delete( $post->ID, 'post_meta' );

	clean_object_term_cache( $post->ID, $post->post_type );

	wp_cache_delete( 'wp_get_archives', 'general' );

	/**
	 * Fires immediately after the given post's cache is cleaned.
	 *
	 * @since 2.5.0
	 *
	 * @param int     $post_id Post ID.
	 * @param WP_Post $post    Post object.
	 */
	do_action( 'clean_post_cache', $post->ID, $post );

	if ( 'page' === $post->post_type ) {
		wp_cache_delete( 'all_page_ids', 'posts' );

		/**
		 * Fires immediately after the given page's cache is cleaned.
		 *
		 * @since 2.5.0
		 *
		 * @param int $post_id Post ID.
		 */
		do_action( 'clean_page_cache', $post->ID );
	}

	wp_cache_set_posts_last_changed();
}