Rank Math Sitemap Not Updating? Why New Posts Are Missing and How to Fix It
If new posts are missing from your Rank Math sitemap, and the sitemap only catches up after you save the sitemap settings or switch a module off and on, one cause to check, apart from any caching plugin, is Rank Math’s own sitemap cache. In Rank Math 1.0.280 the class that clears that cache when content changes is only created for admin requests and during WP-Cron. Posts I created from scripts and the command line left the sitemap stale, which fits that code. This guide shows how to confirm that on your own site, how to clear a stale sitemap right now, and a small must-use plugin that fixes it for good. I found and tested this on WordPress 7.1.3 with Rank Math SEO 1.0.280 on a WordPress site running in Docker behind Cloudflare.
The symptom
You publish or update a post, open /sitemap_index.xml or /post-sitemap.xml, and the post is not there. A few hours or days later it appears, or it appears the moment you touch the sitemap settings. Nothing repairs the cache on a timer, so when the post finally shows up it is usually because some other change cleared the cache. Open the sitemap index and follow the link to the right child sitemap, such as post-sitemap.xml, because the index lists sitemaps, not individual posts. Typical situations:
- Posts published by a script, WP-CLI or the REST API, for example from an automation tool or a publishing workflow.
- Posts created by another plugin, such as a form or import plugin.
- Pages you set to draft or delete from outside the editor, which stay in the sitemap and return 404 to Google.
Stale entries are not only a delay. If a removed page stays listed, Google keeps crawling a URL that now returns an error.
Why it happens
Rank Math builds each sitemap once and saves the XML as a file in wp-content/uploads/rank-math/, named like rank_math_<hash>.xml. Later requests are served from those files. When a post changes, a watcher class is supposed to invalidate them so the sitemap is rebuilt. In the plugin’s sitemap module, the watcher is created like this (from includes/modules/sitemap/class-sitemap.php, version 1.0.280):
if ( is_admin() || wp_doing_cron() ) {
new Cache_Watcher();
}
So the hooks that clear the cache are only registered for admin requests and cron runs. On my site, posts created through scripts and the command line did not clear the cache. I did not test every route; for example, Rank Math has its own admin code for saving posts in the editor, and that code can clear the cache by itself. Treat the code above as the likely reason, not as a full map of every case.
You can check this on your own copy of the plugin:
grep -rn "Cache_Watcher" wp-content/plugins/seo-by-rank-math/includes/modules/sitemap/class-sitemap.php
If your version still has the is_admin() || wp_doing_cron() condition, you have the same behaviour. Newer versions may have changed it, so look before you apply the fix below.
Quick fix: clear the cached sitemap files
To force a rebuild now, delete the cached files. They are only a cache and Rank Math regenerates them on the next request. First check that your site uses file storage: look in wp-content/uploads/rank-math/ for files named rank_math_*.xml. If there are none, Rank Math may be storing the cache in the database instead (as transients), and the stale entries live there, so clearing the files will do nothing. If the files exist, run this from the WordPress folder (inside the container if you use Docker):
rm wp-content/uploads/rank-math/rank_math_*.xml
Then open /sitemap_index.xml in a browser. On my site, calling the plugin’s invalidate_storage() method on its own did not remove the files, while deleting them did, so this is the reliable way. If your site uses a page cache or a CDN, make sure it does not cache the sitemap URLs. I excluded sitemap and .xml in the WP Super Cache settings for that reason, and Cloudflare was not caching the sitemap responses.
A longer-lasting fix: a must-use plugin
This is a version-specific workaround that I tested on one site, so review it after Rank Math updates. It creates the same watcher in the contexts Rank Math skips. Create the file wp-content/mu-plugins/rank-math-sitemap-cache-fix.php (create the mu-plugins folder if it does not exist). Must-use plugins load automatically, with no activation step:
<?php
/**
* Plugin Name: Rank Math sitemap cache fix
* Description: Registers Rank Math's sitemap cache watcher outside admin and cron.
*/
add_action('init', function () {
if (is_admin() || wp_doing_cron()) {
return; // Rank Math already registers its cache watcher here.
}
if (class_exists('\RankMath\Sitemap\Cache_Watcher')) {
new \RankMath\Sitemap\Cache_Watcher();
}
}, 20);
The condition mirrors the plugin’s own, so in the versions I read the watcher is not created twice. The check for the class means the file does nothing if Rank Math is deactivated or the class is renamed. It runs at init priority 20, so a post saved earlier than that in the same request is not covered. A PHP error in a must-use plugin can take the site down, so keep SFTP, SSH or docker exec access ready to delete the file.
Check that it works
- Create a test draft and publish it through the same route that failed before, for example your script or the REST API.
- Open
/post-sitemap.xmland confirm the new URL is listed. - Set the post back to draft the same way and confirm the URL disappears.
Remove the mu-plugin when a Rank Math update makes it unnecessary.
After the sitemap is correct
You do not have to resubmit the sitemap to Google each time. Search Console re-reads a submitted sitemap on its own schedule, and it is enough that the file is correct. For a page you care about, use URL Inspection and request indexing.
Common questions
Does this apply to Yoast SEO or other sitemap plugins?
I only tested Rank Math. Other plugins have their own invalidation rules, and a stale sitemap there needs its own investigation.
Will the mu-plugin slow my site?
It registers Rank Math’s own cache callbacks on front-end requests. I have not measured the cost, so check your own site’s response times after adding it.
Why not just disable the sitemap cache?
You can, but on a large site the sitemap would then be rebuilt from the database on every request. Fixing the invalidation keeps the speed of the cache and the accuracy of a fresh sitemap.
Sources
- Rank Math SEO 1.0.280, file
includes/modules/sitemap/class-sitemap.php(read from the installed plugin). - WordPress Developer Resources: Must-use plugins
