Rank Math Sitemap Not Updating After REST API Edits: Cause and Safe Fix
Almost everything on this site is edited through the WordPress REST API, the HTTP interface WordPress offers for reading and changing content from outside its admin screens. My publishing scripts use it, and so did the content refresh I ran on September 5, 2026, which updated nine pages. Ten days later the sitemap still showed those nine pages with their July dates.
This note explains why. It covers how Rank Math, the SEO plugin that builds the sitemap here, stores sitemaps and decides when to throw them away, how I narrowed the problem down, the small workaround I now run after every publishing batch, how I checked that it works, and what you can check on your own site.
Checked against Rank Math SEO 1.0.278, the version installed when I found the problem, and against the source of 1.0.279, the next release. Everything described below is the same in both. If you run a later version, look at the sitemap module’s constructor in your copy before you rely on this.
What lastmod is for
A sitemap is an XML file that lists a site’s URLs for search engines. Each entry can carry a lastmod value: the time the page last changed in a meaningful way. Crawlers have far more pages to visit than time to visit them, so a lastmod they can trust tells them which pages are worth fetching again. Google’s sitemap documentation says it uses the value when it is consistently and verifiably accurate, for example when it matches the last time the page actually changed.
That condition cuts both ways. A sitemap that shows old dates for pages that changed is not harmless. It tells a crawler that nothing happened, and if the dates are wrong often enough, they stop being worth reading at all. Faking freshness is just as bad. So the goal was never “newer dates”. The goal was dates that match what actually changed.
A check that passed and missed it
The September 5 refresh ended with a verification script. It checked status codes, canonical URLs, page descriptions, whether each page could be indexed, every link and image, and whether every edited page was listed in the sitemap. Everything passed. All of it was true, and the sitemap was still wrong. The script asked whether each URL was in the sitemap. It never asked what date the sitemap gave it.
On September 15 I reviewed the site again and this time compared dates. For each of the 22 public pages I took the modified time the REST API reports and put it next to the page’s lastmod in the page sitemap. Thirteen matched to the second. Nine did not, and those nine were exactly the pages changed on September 5. The home page, for example, had been modified at 09:24:21 UTC on September 5. The sitemap said 16:36:36 UTC on July 29. The sitemap index, which lists each sub-sitemap with a date of its own, still said July 29 as well.

One detail is easy to get wrong when you make this comparison yourself. WordPress reports two times for every page: modified, in the site’s own time zone, and modified_gmt, in UTC. This site’s time zone is set to New York, so in summer the two differ by four hours. Rank Math writes lastmod in UTC with an explicit +00:00 offset. Compare it with the GMT value, or every page will look four hours off and the real problem will be buried in noise.
Narrowing it down
A stale sitemap can come from several layers: the browser, a proxy or CDN in front of the site, a page-caching plugin, the SEO plugin’s own cache, or a static sitemap file left on disk. Before touching anything I wrote these suspects down, together with one rule for the repair: change only the layer that is actually at fault, and never re-save pages just to give them a new date.
The outer layers went first. I fetched the sitemap again with a throwaway query string, which makes most proxies treat it as a new address. The host’s proxy reported a cache miss with an age of zero seconds, which means it had just asked WordPress for a fresh copy. The XML was byte-for-byte identical to the first download. Whatever was serving old data was inside WordPress.
The most useful clue came from looking at the wrong values themselves, not just at the fact that they were wrong. Before the September 5 refresh I had exported every page as a backup. Each stale date in the sitemap was that page’s modified time from the backup, to the second. A bug in how dates are calculated would produce strange dates. This was a correct answer from the past being repeated: a snapshot. The newest date anywhere in the sitemap was 16:36:40 UTC on July 29, the last edit of a round of changes I made that day. So the snapshot had been built some time after that edit and before September 5, and nothing had replaced it since.

A snapshot pointed at the plugin’s own cache, so I read the plugin.
How Rank Math stores a sitemap
Rank Math does not build a sitemap on every request. The first time a sitemap is requested, it queries the database, builds the XML and stores the result. Every request after that is answered from storage until the stored copy is deleted.
There are two kinds of storage. If the plugin can write to its own folder inside the WordPress uploads directory, each sitemap is saved there as a file. If it cannot, the sitemap goes into the database as a transient, WordPress’s name for a cached value with an expiry time. The storage key is made from the sitemap type, the page number and the site’s home address, so the page sitemap, its second page and the sitemap index are each stored separately. Files have no expiry at all. Transients are set to expire after 100 days.
That detail explains why the problem did not go away on its own. A cached sitemap here is not refreshed after an hour or a day. In file mode it stays until something deletes it. In database mode it would have taken more than three months. Any crawler that read this site’s sitemap during those ten days got the July snapshot.
Caching is a reasonable design. On a large site a sitemap can mean thousands of database rows, and crawlers ask for sitemaps often. But the design only works if every change to content reliably deletes the stored copies it affects.

Who deletes the stored copy
Deleting is the job of a class Rank Math calls the cache watcher. When it is loaded, it attaches itself to WordPress hooks, the named events that let a plugin run code when something happens:
- a post being saved, and a post being published;
- categories and tags being edited, or attached to and removed from posts;
- users being registered, updated or deleted, which matters for author sitemaps;
- changes to a short list of options: the site address, the permalink structure, the date format, the list of active Rank Math modules, and Rank Math’s own option groups.
A few saves are skipped on purpose: revisions, auto-drafts and password-protected posts. For everything else the watcher does not delete anything straight away. It adds the affected sitemap type to a queue, always together with the index, because the index carries a date for every sub-sitemap. When the request finishes, on WordPress’s shutdown hook, it deletes everything in the queue at once. So a bulk edit of fifty posts in one request deletes the page sitemap once, not fifty times.
All of that is sound. The problem is the line that creates the watcher. In the sitemap module’s constructor, the watcher is created only when one of two WordPress checks is true. The first is is_admin, which is true for requests to the admin screens under wp-admin. The second is wp_doing_cron, which is true while WP-Cron, WordPress’s scheduled-task runner, is working through its jobs. A REST request is neither. It goes to the site’s /wp-json/ address and runs without the admin area.
So when my script saves a page through the REST API, WordPress does everything it normally does. The page is updated, its modified time changes and the save hooks fire. But in that request there is no watcher to hear them. Nothing is queued, nothing is deleted, and the next sitemap request is answered from the old copy.
A later cron run does not repair it either. A cron request does create a watcher, but a watcher only reacts to events that happen during its own request. Its queue lives in memory and starts empty. There is no record anywhere of the saves made earlier in other requests, so there is nothing for it to catch up on.

This also explains why the July 29 edits did show up in the sitemap. Something cleared the cache after the last of them. I did not find out what it was, and it matters less than it seems. A single clear makes every date correct at that moment, and the next REST edit goes stale again. The sitemap shows the state of the site at the last clear, not its current state.
I did not test the block editor. It also saves through the REST API, so on paper it could have the same gap, but an editing session involves other admin requests too, and I have not checked whether any of them reaches the watcher. Everything in this note is about direct REST clients such as publishing scripts.
The hook that runs in every request
The same constructor registers one more cache action, this time with no condition around it. Whenever Rank Math finishes saving its settings, it fires a hook named rank_math/settings/after_save, and the sitemap module answers by deleting every stored sitemap: the whole cache folder, every sitemap transient, and the list of cached files it keeps. Because this registration sits outside the admin-or-cron check, it exists in REST requests too.
Rank Math saves its settings through its own REST endpoint, updateSettings in the plugin’s rankmath/v1 namespace. Its settings screens use the same endpoint behind the scenes, and it requires a user who is allowed to manage Rank Math settings. The handler cleans up the submitted values, merges them over the stored settings for that group, writes the whole group back, and then fires the after-save hook. So a genuine settings save, sent by a script that has the right permission, makes Rank Math throw away its sitemaps through a path the plugin itself supports.
Two properties of that save shaped the workaround. First, the plugin writes back the entire option group and fills in whatever I did not send from the stored values. If I sent a full copy of the sitemap settings and part of it was out of date, I could overwrite a real setting. Sending a single field avoids that. Second, the handler’s “nothing changed” check only stops a save when there are no valid settings at all, so even re-sending the current value would fire the hook. I change the value anyway, because a changed value can be confirmed. If I send 201 and the reply says 201, I know the save happened in that request. If I sent 200 and got 200 back, the reply would prove nothing.
The workaround
The publisher on this site, the script that sends pages and posts to WordPress, now ends each publishing run with a short refresh. It uses the sitemap setting “links per sitemap”, which sets how many URLs go into each sitemap file. Here it is 200, far more than the 22 pages the site has, so a change of one makes no visible difference to any sitemap.
- Export only the sitemap settings through Rank Math’s settings export, and read the current value.
- Check it before changing anything. It must be a whole number from 1 to 50,000. The upper bound is the sitemap protocol’s own limit of URLs per file. Anything else means the export did not return what I expect, and the refresh stops without saving.
- Save that one field as the value plus one, or as 49,999 if it is already at the maximum, and check that the reply contains the new value.
- Save the original value back and check the reply again. This step is written so that it runs whatever happens in step three, including an error or a reply that does not say what it should.
Both saves fire the after-save hook, so the cache is deleted twice. The first deletion is the one that matters. The second also removes any sitemap a crawler happened to request in the moment between the two saves. On a site with more than 200 URLs of one type, that sitemap would have split its pages at 201. On this site it would have looked exactly the same.

Where the refresh runs matters as much as what it does. It runs after the post has been saved and after its new state, including the post ID WordPress assigned, has been written into the local publishing record. If the refresh fails, the run reports an error, but the ID of a newly created post is never lost. It also runs only when publishing. Drafts are not in the sitemap, so there is nothing to refresh for them. Any other script that writes to the site through REST has to call the same helper at the end of its own batch. The plugin itself is unchanged.
Three automated tests cover the helper. They replace the network layer with a fake that records what would have been sent. The first checks a normal refresh: one export, then exactly two saves of a single field, 201 and then 200. The second makes the first save fail with an error and checks that the restore still happens, so the recorded saves are again 201 and then 200. The third makes the export return a value that is not a number and checks that exactly one request was made, the export, and nothing was written.
What I chose not to do
Re-saving every page would have given each one a new modified time, and the sitemap would have been rebuilt along the way. It would also have made lastmod claim that 22 pages changed when nine had, which is exactly the kind of inaccuracy I was trying to remove.
Patching the plugin to create the watcher in REST requests as well is a one-line change. The next plugin update would silently undo it, and I would only find out when the dates went stale again. Turning sitemap caching off is possible through a filter Rank Math provides, but it needs custom code of my own on the server, makes every crawler request rebuild every sitemap, and hides the gap instead of working with it. Opening the admin and saving a setting by hand after each batch would also work. It is a manual step in a process whose whole point is that it runs without me.
How the fix was verified

The repair went live on September 15 together with a batch of other edits, which made a useful test, because seven content pages changed that day. After the refresh, the sitemap timestamp of each of the seven matched its modified_gmt value exactly. Their new dates read September 14: the edits were made on September 15 in Korea, which was still September 14 in UTC. No date was moved by hand. Pages that had not been edited kept their old dates, which is what should happen.
The /play/ hub, which now redirects to the games page, was excluded from the sitemap as intended. The links-per-sitemap setting read 200 afterwards, as before. The three tests passed.
One side effect is worth knowing if you compare exported settings. Because a settings save writes the whole group back, some sitemap options that had been stored as “on” and “off” came back as true and false. They mean the same thing to the plugin, but a plain comparison of the exports before and after will flag them as changes.
Checking your own site
If anything writes to your WordPress site through the REST API, whether a publishing script, a mobile app, a headless front end or an automation service, this takes about ten minutes:
- Pick a page you changed recently through that route. Read its modified_gmt from the REST API, for example by asking the pages endpoint for the page’s slug, and compare it with its lastmod in your sitemap. Use the GMT value.
- If they differ, compare several pages. If every page edited after a certain moment is stale, and each stale date is that page’s previous modified time, you are looking at a stored snapshot, not a date bug.
- Rule out the caches in front of WordPress. Request the sitemap with a throwaway query string and read the response headers your host or CDN adds.
- Find out which plugin builds your sitemap and how it clears its cache. In Rank Math, look at the condition around the cache watcher in the sitemap module’s constructor.
- Try the plugin’s own clear. In Rank Math, saving the sitemap settings in the admin fires the after-save hook. Request the sitemap again and compare dates. Rank Math’s own sitemap troubleshooting page covers caching plugins and other causes that were not involved here.
- If you automate the clear, use a path the plugin supports, change as little as possible, put things back even when a step fails, and verify by comparing dates afterwards. Do not repair a sitemap by re-saving pages so that their dates look newer.
Update, November 2026. The publisher for these Dev Log notes, added in October, calls the same refresh after each batch of notes.