![]()
A custom post can be saved successfully and still display old content when a cache serves an earlier page. To Clear WP Rocket Cache automatically, connect the correct post-type save hook to WP Rocket’s per-post purge function, then clear Autoptimize’s optimization cache only if the saved content can affect optimized assets.
These plugins manage separate layers: WP Rocket caches page output, while Autoptimize generates optimized CSS and JavaScript files. Clearing one does not automatically purge the other, so this guide keeps the page purge targeted and the broader Autoptimize operation optional. Test the change on staging before relying on it in production.
- A WordPress staging site or a recent, restorable site backup.
- WP Rocket and Autoptimize installed and active, with their cache roles understood.
- PHP 8.0+ and permission to add a site-specific plugin or edit a child theme.
- The registered custom post type key, such as
resource, rather than its public label. - A logged-out browser or private window for checking front-end output.
Step 1: Verify the custom post type and cache layers
Before adding a purge callback, confirm the custom post type key and identify which cache is serving the stale output. A post save may publish or edit an item, or change its status; the callback must account for status changes rather than skipping every post that is not currently published.
Identify the registered post type key
Find the key passed as the first argument to register_post_type(). If the registration uses resource, WordPress provides the post-type-specific hook save_post_resource. Use that exact key—not a display label such as Resources or a rewrite slug that may differ. Replace resource with your own registered key when implementing the hook.
Map each plugin to the cache it clears
WP Rocket stores cached page output for URLs. Its rocket_clean_post($post_id) function targets the saved post, helping refresh that post’s page cache. Autoptimize instead generates optimized CSS and JavaScript files; by default, it is not another full-page cache. Its autoptimizeCache::clearall() method clears the optimization cache broadly, not just one post URL, and does not replace the WP Rocket page purge. Use that wider operation only when the content can affect optimized assets or testing shows it is required.
Identify the registered post type key
The identifier for the save hook is set when the custom post type is registered. Find the register_post_type() call in the site-specific plugin, theme functionality, or plugin responsible for registering the content type. If a graphical post-type registration tool is used, look for its internal post type key in the settings. In register_post_type('resource', $args), for example, resource is the key: it is the first argument passed to the function.
Build the hook name from that key, not from the label displayed in the WordPress admin menu. A label such as Resources may be pluralized or translated, while a front-end rewrite slug can be configured separately. For the example key resource, the hook is save_post_resource. Replace resource with the exact registered key used by your site, preserving its spelling, before adding the callback. This check prevents a correctly written callback from silently listening to the wrong post type.
Map each plugin to the cache it clears
WP Rocket and Autoptimize do different jobs, so a purge in one plugin does not replace a purge in the other. For the saved custom post, rocket_clean_post($post_id) is WP Rocket’s targeted operation: it clears the page-cache entry associated with that post, rather than requiring a routine full-site purge. WP Rocket may also handle related cached URLs according to its own cache logic.
Autoptimize’s autoptimizeCache::clearall() clears its generated optimization cache as a whole. That cache contains optimized assets such as CSS and JavaScript files; the call is not a purge for one post URL and does not clear WP Rocket’s cached page HTML. Keep the Autoptimize call only when saving this content can change optimized asset output, or when testing confirms the site needs those files rebuilt. If the post changes ordinary text or metadata only, the targeted WP Rocket purge is generally the relevant action.
Step 2: Add a guarded custom post type save hook
Place the callback in a small site-specific plugin or an existing functionality plugin; avoid editing a parent theme, where an update could erase the change. WordPress documents the save_post hook. Its post-type-specific version, such as save_post_resource, passes the post ID, post object, and update flag to your callback.
Guard and install the callback
Use the registered post type key in both the hook and type check. This complete example skips autosaves and revisions, then performs the targeted WP Rocket purge. Keep the Autoptimize call only if your setup needs its broader optimization-cache rebuild.
<?php
add_action('save_post_resource', 'pnet_clear_resource_caches', 10, 3);
function pnet_clear_resource_caches($post_id, $post, $update) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) {
return;
}
if (!($post instanceof WP_Post) || 'resource' !== $post->post_type) {
return;
}
if (function_exists('rocket_clean_post')) {
rocket_clean_post($post_id);
}
if (class_exists('autoptimizeCache')) {
autoptimizeCache::clearall();
}
}
The callback deliberately does not require a published status: status changes can affect cached output too. If saving this post type cannot affect optimized assets, remove the Autoptimize block. Replace resource in both locations with your exact registered post type key.
Guard the callback and install the implementation
Add this code to a site-specific plugin or your existing functionality plugin so the callback remains active if you change themes. It listens only for saves to the resource post type, ignores autosaves and revisions, and checks that the received object is the expected post before calling either plugin API.
<?php
add_action('save_post_resource', 'pnet_clear_resource_caches', 10, 3);
function pnet_clear_resource_caches($post_id, $post, $update) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) {
return;
}
if (!($post instanceof WP_Post) || 'resource' !== $post->post_type) {
return;
}
if (function_exists('rocket_clean_post')) {
rocket_clean_post($post_id);
}
if (class_exists('autoptimizeCache')) {
autoptimizeCache::clearall();
}
}
The callback accepts the post ID, post object, and update flag required by the hook. It intentionally does not require a published status: publishing, editing, or changing status may all affect cached output. Keep the guarded Autoptimize block only if saving this content can change optimized assets or testing shows a rebuild is needed. Replace resource in both the hook and post-type check with your exact registered key.
Pro Tip

Did you know: You can explore our complete guide on exclude scripts in WP Rocket for step-by-step details.
Step 3: Deploy safely and verify the purge
Install the snippet on staging, then save a custom post and check that it saves normally without PHP warnings or errors. For a published item, change a visible title or add a distinctive test phrase to its body, save, and open its public URL in a private browser window where you are logged out. Confirm the new content appears. Logged-in administrators can bypass page caching or see different cache behavior, so their view alone is not a reliable test.
Verify the page and optimization cache separately
Also test saving a draft and changing a published item to draft. The callback should not depend on the post being published at the instant the save hook runs. If the public page still shows old content, manually purge that page once through WP Rocket and reload the logged-out view. If that fixes it, investigate whether the callback ran and whether the correct post ID was passed; if not, check for another cache layer.
Judge WP Rocket page output separately from Autoptimize CSS and JavaScript files. A rebuilt asset cache does not prove the page HTML was refreshed. If plugin-level purging works but the page remains stale, check any CDN, hosting cache, or reverse proxy and purge that layer as a separate test.
Verify the page and optimization cache separately
Add a distinctive marker, such as Cache check 14:32, to the resource post, save it, and open its public URL in a logged-out private window. Confirm the marker appears. This makes the result observable without relying on an administrator view, which may bypass or behave differently from the public page cache.
Check WP Rocket’s page output independently from Autoptimize’s generated CSS and JavaScript files. A fresh or regenerated Autoptimize asset does not confirm that WP Rocket served updated page HTML; likewise, current page text does not prove optimized assets were rebuilt. Keep the two results separate when diagnosing a stale display.
If the plugin-level checks look correct but the public URL remains outdated, investigate any CDN, hosting cache, or reverse proxy. Those layers are outside the two plugin calls in the example and may need a separate purge. Once the test passes, remove the temporary marker.

Step 4: Keep cache purging targeted and efficient
For routine edits, rocket_clean_post($post_id) is the appropriate WP Rocket operation: it targets the saved post instead of purging the entire site. A site-wide purge can discard useful cached pages that the edit did not affect, increasing cache rebuild work for visitors and the server.
Choose the right purge scope for each plugin
Keep the per-post WP Rocket purge for ordinary content updates. Autoptimize’s autoptimizeCache::clearall() clears generated optimization files broadly, not just assets associated with one post. On a busy editorial site, running it on every save can trigger unnecessary rebuilds and repeated cache misses.
Retain the guarded Autoptimize call only when saving this custom post can change optimized asset output, or staging tests show that those files must be rebuilt. The function and class checks protect against errors when a plugin is inactive; they do not mean the purge is needed. If the post changes only text or metadata, omit the Autoptimize operation.
Pro Tip
You might also like:
Choose the right purge scope for each plugin
For ordinary edits to a custom post, keep the targeted WP Rocket call, rocket_clean_post($post_id). It clears the page cache associated with the saved post without discarding cached pages across the site. This is usually the right scope when a title, body, or metadata change affects that post’s rendered page. Avoid a domain-wide purge on every save; use one only when a specific change has wider effects, such as shared output used across many pages.
Decide separately whether the save can affect Autoptimize’s generated CSS or JavaScript. If the post only changes text and metadata, remove the Autoptimize clearall() call. Keep it, behind the class check, only when asset references or generated asset output can change and a staging test confirms that rebuilding the broader optimization cache is necessary. Frequent editorial saves can otherwise cause avoidable asset regeneration and cache misses. The function and class guards prevent fatal errors when a plugin is inactive; they are safety checks, not evidence that either purge is required.
Common Errors: Troubleshoot stale custom post output
The hook does not run or purges the wrong post
Likely cause: The hook uses a public label or rewrite slug instead of the registered post type key, or the file containing the callback is inactive. Fix: Confirm the key and use the matching hook, such as save_post_resource. On staging, temporarily log the post ID and post type from a pnet_-prefixed diagnostic callback, then remove the diagnostic.
The page stays stale or PHP reports an error
Likely cause: The purge callback is not reached, a plugin API is unavailable, or a CDN, host cache, or reverse proxy still serves old output. Fix: Keep the function_exists and class_exists checks, verify plugin activation, then test WP Rocket by itself. If its purge runs but output is still stale, check other cache layers before changing the callback. Autoptimize rebuilding assets will not refresh cached page HTML.
Autoptimize rebuilds too often
Likely cause: autoptimizeCache::clearall() runs on every save, although the post does not affect optimized assets. Fix: Remove that optional call or narrow when it runs. Test one cache layer at a time so you can identify which purge resolves the stale output.
You might also like:
The hook does not run or purges the wrong post
Check the hook name first. It must use the registered post type key exactly: if the first argument to register_post_type() is resource, use save_post_resource. A menu label such as Resources or a rewrite slug may be different. Also confirm the file containing add_action() is active and loaded; a callback placed in an inactive plugin or theme file cannot run.
If the key appears correct, use a temporary diagnostic on staging to see which post ID and type WordPress passes when you save. It listens to saves generally so it can reveal a mismatch:
<?php
add_action('save_post', 'pnet_log_saved_post_type', 10, 3);
function pnet_log_saved_post_type($post_id, $post, $update) {
if (!($post instanceof WP_Post)) {
return;
}
error_log('Saved post ID: ' . absint($post_id) . '; type: ' . $post->post_type);
}
Check the staging PHP error log after saving the affected item, then remove this diagnostic callback. Match the reported type to both the hook suffix and the post-type check in the purge callback. Do not leave broad save logging enabled on a production site, where it can create unnecessary log entries.
The front end is still stale or PHP reports an error
If the page remains stale: Confirm the callback runs and reaches rocket_clean_post($post_id). On staging, temporarily log the post ID immediately before that call, then remove the diagnostic after testing. Manually purge the affected page once in WP Rocket: if it updates, investigate the hook or callback; if it does not, check for a CDN, hosting cache, or reverse proxy that also needs purging.
If PHP reports an undefined function or class: Keep the function_exists('rocket_clean_post') and class_exists('autoptimizeCache') guards. Verify the relevant plugin is active and that the callback is registered in an active site plugin or theme file. Check the PHP error log for the exact missing name and test the change on staging before deploying it.
If Autoptimize files are rebuilt but page text is old: The asset optimization cache and cached page HTML are separate layers. A rebuilt CSS or JavaScript file does not refresh the WP Rocket page cache; test the page purge and other cache layers separately.
If saves trigger frequent, expensive rebuilds: Remove the optional Autoptimize clearall() call or run it only when the saved content can affect optimized assets. Do not broaden the WP Rocket purge to solve an unrelated asset-cache issue.
Conclusion: Purge the right cache after each save
A stale custom post page usually means the page cache still holds an earlier version of its output. Connect the exact post-type-specific save hook, such as save_post_resource, to a guarded callback that checks for autosaves and revisions, verifies the post object, and calls WP Rocket’s per-post purge when available. This refreshes the saved post without unnecessarily discarding the whole site cache.
Treat Autoptimize separately: its clearall() method rebuilds the broader optimization cache, so keep it only if saving this content can affect optimized assets or testing proves it is needed. Before deploying, test normal edits, drafts, status changes, and logged-out front-end output on staging. If a page remains stale after the plugin purge, investigate hosting, CDN, or reverse-proxy caches as well. With those checks complete, **Clear WP Rocket Cache** at the right scope after each relevant save.