The clean_page_cache() WordPress PHP function is used to delete a specific page from the cache that matches a given $id
. This function also cleans the cache associated with ‘all_page_ids’ and ‘get_pages’. You might also be interested in a similar function, clean_post_cache()
.
Usage
clean_page_cache( $id );
Example:
clean_page_cache( 42 );
In this example, the cache for the page with ID 42 will be cleaned.
Parameters
$id
(int) – The ID of the page that you want to clean from the cache.
More information
See WordPress Developer Resources: clean_page_cache()
This function is a part of WordPress core and is typically used when you need to ensure that the cached version of a page is up-to-date with the latest content.
Examples
Clearing cache for a single page
If you’ve updated the content for a page and want to ensure that visitors see the updated content, you can use this function to clear the cache for that page.
// Page ID is 57 clean_page_cache( 57 );
In this example, the cache for page 57 will be cleared.
Using in a loop
If you have a list of page IDs and you want to clean the cache for all these pages, you can use this function in a loop.
// Array of page IDs $page_ids = array( 1, 2, 3, 4, 5 ); // Loop through each page ID foreach ( $page_ids as $page_id ) { clean_page_cache( $page_id ); }
This example will clean the cache for pages with IDs 1, 2, 3, 4, and 5.
Clearing cache after updating page metadata
If you’ve updated metadata for a page, you can use this function to ensure that the cache reflects these updates.
// Assume $page_id is the ID of the page // Assume $meta_key and $meta_value are the metadata key and value // Update page metadata update_post_meta( $page_id, $meta_key, $meta_value ); // Clear page cache clean_page_cache( $page_id );
This code updates the metadata for a given page and then clears the cache for that page.
Clearing cache when using wp_update_post
If you’re using wp_update_post
to update a page, you can use this function to clear the cache for that page.
// Assume $page_id is the ID of the page // Assume $page_data is an array of data to update the page // Update the page wp_update_post( $page_data, true ); // Clear page cache clean_page_cache( $page_id );
This code updates a given page using wp_update_post
and then clears the cache for that page.
Clearing cache after a scheduled event
If you have a scheduled event that updates pages, you can use this function to clear the cache for those pages after the event.
// Assume $page_id is the ID of the page // Assume 'my_scheduled_event' is a scheduled event that updates pages // Add an action to clear cache after the scheduled event add_action( 'my_scheduled_event', function() use ( $page_id ) { clean_page_cache( $page_id ); } );
This code sets up an action to clear the cache for a given page after a scheduled event runs.