📝 Fix Multisite Subdomain Mapping to the Wrong Blog
A WordPress Multisite network running subdomain installs resolves each incoming request to a blog_id through a lookup that gets cached in the “blog-lookup” object cache group. When a site’s domain mapping changes — a new primary domain, an updated DNS record, or a direct database edit to wp_blogs/wp_site outside of WordPress’s own site-update functions — a Redis or Memcached-backed object cache can keep serving the OLD blog_id for that domain until that specific cache entry expires on its own, because a raw UPDATE statement never fires the WordPress action that would normally clear it.
🔎 The Problem
wp_blogs row updated directly via a migration script: blog_id 7, domain changed from old.example.com to new.example.com Object cache (Redis), group "blog-lookup", key "new.example.com": still resolves to blog_id 3 (a different, unrelated site) because that key was cached from an earlier request and nothing told the cache the mapping table changed underneath it.
✅ Fix: Update Through the API, Then Flush the Cache
- Make domain-mapping changes through WordPress’s own API (`wp_update_site()` or the Network Admin “Edit Site” screen) rather than editing wp_blogs/wp_site directly in the database, since only the API call fires the actions that clear the relevant object cache keys.
- If a direct database edit already happened, manually flush the “blog-lookup” cache group (or run a full cache flush if that’s not exposed) instead of assuming the new row will simply be picked up on the next request.
- After remapping, confirm the fix with `get_blog_id_from_url()` or by visiting the domain in a private window with cache busting, since a browser or CDN-level cache of the previous site’s response can mask whether the server-side fix actually worked.
🚨 Why This Is Dangerous
- A visitor — or a login form — silently landing on the wrong network site after a domain change can mean a password typed into the wrong site’s login screen, or one client’s content appearing under another brand’s domain, invisible until someone actually clicks through.
The database row can be correct the instant you save it; the cache in front of it decides when that becomes true for visitors.
