Purging additional paths when clearing cache
You can extend Pressidium’s automatic Page Cache purge behavior so that additional URL paths are cleared whenever a related cache purge is triggered.
This is useful when updating one WordPress post or page also changes content displayed elsewhere, such as an archive, listing page, custom post-type index, or another related URL that is not included in the default purge operation.
What this guide covers
This guide explains how to use the pressidium_purge_page_cache filter to:
- Add additional paths to the automatic Page Cache purge list
- Define affected paths with regular expressions
- Add paths based on the post type that triggered the purge
- Inspect the post IDs associated with a purge operation
This customization changes which paths are cleared during future purge operations. It does not exclude those paths from caching.
When to use
Use this customization when changes to one piece of WordPress content must automatically clear cached responses for other related paths.
For example, use it when updating:
- A post also changes a custom listing page
- A custom post type also changes its archive
- Content appears on several related landing pages
- A page update affects a URL that Pressidium does not purge automatically
For a one-time cache refresh, use Purge URLs from Page Cache instead.
To prevent a URL from being cached at all, use a Page Cache exclusion instead.
Prerequisites
Before continuing:
- Make sure you are comfortable editing PHP.
- Confirm which additional paths must be purged.
- Make sure you understand how the regular expressions will match your website’s paths.
- Test the customization on a clone or non-production environment before using it in production.
- Create a backup before changing theme or plugin code.
You can add the filter to your theme’s functions.php file or to a custom plugin. Use a custom plugin when the purge behavior should remain independent of the active theme.
Add additional paths to the purge list
If you need to purge additional paths, you can hook into the pressidium_purge_page_cache filter (Read more on Pressidium specific WordPress actions and filters). You can add this functionality in your functions.php file or in a custom plugin.
Sample code to add paths to be purged to the existing path list:
/**
* Filter computed paths that are going to be purged.
*
* @param array $paths Paths to be purged.
*
* @return array
*/
function mycustom_purge_additional_paths( $paths ) {
$paths[] = '^/foo/';
$paths[] = '^/bar/';
return $paths;
}
add_filter(
'pressidium_purge_page_cache',
'mycustom_purge_additional_paths'
);
Paths are regular expressions
We strongly recommend to use the ^ and/or $ anchor tokens to match the beginning and end of a path (check out regex101 to test your regular expressions).
Most paths should begin with the ^ token to match the beginning of the path.
/foo/will invalidate/foo/,/foo/bar/, and/baz/foo/.^/foo/will invalidate/foo/and/foo/bar/, but it won’t invalidate/baz/foo/.
You can inspect the post(s) that triggered the cache purge operation by looking into the second parameter of the filter. It contains an array of post IDs that can be used to get additional post information.
Sample code to add paths to be purged depending on post type:
/**
* Filter computed paths that are going to be purged.
*
* @param array $paths Paths to be purged.
* @param array $post_ids IDs of the posts/pages to be purged.
*
* @return array
*/
function mycustom_purge_additional_paths( $paths, $post_ids ) {
foreach ( $post_ids as $post_id ) {
$post = get_post( $post_id );
switch ( $post->post_type ) {
case 'post':
$paths[] = '^/foo/';
break;
case 'my_custom_post_type':
$paths[] = '^/bar/';
break;
default:
break;
}
}
// Return unique paths
return array_values( array_unique( $paths ) );
}
add_filter(
'pressidium_purge_page_cache',
'mycustom_purge_additional_paths',
10,
2
);
Expected outcome
When a matching cache purge is triggered, Pressidium adds the custom paths returned by the pressidium_purge_page_cache filter to the existing purge list.
The cached responses for those paths are removed. The next request generates a fresh response, which is then cached again according to your website’s cache configuration.
Verification
After adding or changing the filter:
- Update a post or page that should trigger the custom purge behavior.
- Wait for the cache purge operation to complete.
- Open each additional path in a private or incognito browser window.
- Confirm that the latest content appears.
- Test a similar path that should not match the regular expression.
- Confirm that unrelated cached pages were not purged unexpectedly.
To inspect the cache status of a response, follow Identifying Cache Behavior Using HTTP Headers.
Common issues and solutions
An additional path is not purged
Check whether:
- The regular expression matches the requested path.
- The path begins with
/. - The filter code is loaded by the active theme or plugin.
- The callback returns the updated
$pathsarray. - The post type used in the condition matches the post being updated.
- The filter accepts the correct number of parameters.
Test the regular expression before changing the code again.
More paths are purged than expected
The regular expression may be too broad.
Use the ^ anchor to match the beginning of a path and the $ anchor when the rule should match only one exact path.
For example:
'^/foo/$'
matches only /foo/, while:
'/foo/'
may also match paths containing /foo/ elsewhere.
The website shows an error after adding the code
The custom PHP may contain a syntax error or reference an unavailable value.
Remove or revert the code and restore the last working version. Test the customization on a clone before adding it to production again.
The customization stops working after changing themes
Code added to a theme’s functions.php file is loaded only while that theme is active.
Move the filter to a custom plugin when the purge behavior should remain active independently of the website’s theme.
Duplicate paths appear in the purge list
Use array_unique() before returning the path list:
return array_values( array_unique( $paths ) );
Need help?
Contact Support before changing the purge logic if:
- You are unsure which paths should be purged together.
- The regular expression matches more paths than expected.
- Additional paths are not cleared.
- A custom post-type condition does not work.
- The purge operation causes unexpected performance behavior.
Include:
- The affected domain
- The path that triggers the purge
- The additional paths you expect to be purged
- The regular expressions used
- The complete filter callback
- The relevant post type
- The expected behavior
- The actual behavior
Where to go next
- Use Cache Management to clear cached content manually.
- Follow Purge URLs from Page Cache when you need a one-time purge for selected paths.
- Read Pressidium-Specific WordPress Actions and Filters to review other available integration hooks.
- Read About Page Cache to understand what the Page Cache stores and when it is cleared.
- Follow Identifying Cache Behavior Using HTTP Headers to confirm whether a response is served from cache.
Was this article helpful?