Skip to content

Clearing the Cache Dynamically

Raam Dev edited this page Aug 26, 2014 · 10 revisions

Purge entire cache when saving any post

You can call any class method that is declared as a public function in quick-cache.inc.php or quick-cache-pro.inc.php using $GLOBALS['quick_cache']->any_public_method().

For example, if you wanted to clear the cache every time a post was saved or updated (i.e., when the save_post action is fired), you could add the following to your theme's functions.php file:

add_action( 'save_post', 'my_custom_clear_cache', 10, 1 );

function my_custom_clear_cache( ) {
    $GLOBALS['quick_cache']->clear_cache();
}

Purge page cache when Custom Post Type is saved

Using the save_post_{$post_type} hook, which is fired whenever that custom post type is created or updated (see docs), you can add something like the following to your theme's functions.php file, or to a MU-Plugin, to clear the cache for a given page whenever a post with that custom post type is saved:

add_action( 'save_post_my-custom-post-type', 'clear_cache_for_page_id_5', 10, 1 );

function clear_cache_for_page_id_5( ) {
	$GLOBALS['quick_cache']->auto_purge_post_cache(5);
}

You'll want to change 5 to the ID of the Page/Post whose cache you'd like to clear when that custom post type is saved, and you'll want to change my-custom-post-type to the actual name of your Custom Post Type.

Purge a specific page cache when saving any post

Using the save_post hook, which is fired whenever a post created or updated (see docs), you can add something like the following to your theme's functions.php file, or to a MU-Plugin, to clear the cache for a given page

add_action( 'save_post', 'clear_cache_for_page_id_5', 10, 1 );

function clear_cache_for_page_id_5( ) {
	$GLOBALS['quick_cache']->auto_purge_post_cache(5);
}

You'll want to change 5 to the ID of the Page/Post whose cache you'd like to clear.