WordPress does not keep your site URL in one tidy place. It is in wp_options twice, in the body of every post, in every meta row your page builder wrote — and, critically, inside serialized PHP, where each string is stored behind a byte-length prefix. s:26:"http://staging.example.com" means “a string of exactly 26 bytes follows.” Run a plain UPDATE ... SET col = REPLACE(col, 'old', 'new') and MySQL happily swaps the bytes while leaving that 26 alone. The prefix now lies about the string it describes, unserialize() refuses the whole value, and get_option() quietly hands back false. Your widgets empty out, theme options reset to defaults, builder layouts render blank — and nothing in the error log says why. wp search-replace exists for exactly this reason: it walks each row, detects serialized values, unserializes them, replaces inside the real strings, and reserializes with correct lengths. That is the entire argument for using it instead of SQL.
What you’ll accomplish
- Move a site’s URL between environments — staging to production,
httptohttps, or an outright domain change — without corrupting serialized options, post meta, or builder payloads. - Preview every write with
--dry-runand read WP-CLI’s replacement table before a single row changes. - Cover what a default run misses: plugin tables outside WordPress’s registry, multisite networks, and the one column you must never rewrite.
- Place the replacement at the correct point in a migration — after the import, before the cache flush and permalink rewrite.
- Verify the result at three levels: the options, the raw database, and the front end.
Prerequisites
- WP-CLI installed and talking to the site.
wp --infoshould print your PHP binary and WP-CLI version. Run from the WordPress root,wp core versionshould print something like6.8.1. Ifwpcannot find the install, point it at one:wp --path=/var/www/example.com/public core version. - A database dump, taken before anything else.
search-replacehas no undo. This is the whole safety net:
wp db export ~/backup-pre-replace.sql
Success: Exported to '/home/deploy/backup-pre-replace.sql'.
- Shell access on a host that can reach the database. This is a CLI tool, not a plugin.
- Multisite note. On a network install, every command below needs
--url=<site-url>to select a site, and usually--networkto reach network-wide tables. There is a dedicated troubleshooting entry on this below — read it before you start.
How it works
PHP’s serialize() turns arrays and objects into a flat string so they fit in a single database column. WordPress leans on this constantly: widget assignments, theme mods, most plugin settings, and nearly everything a page builder saves are serialized blobs sitting in option_value or meta_value. The format is self-describing and length-counted — a:1:{s:3:"url";s:26:"http://staging.example.com";} declares a 1-element array whose key is a 3-byte string and whose value is a 26-byte string.
That length prefix is the whole problem. It is computed at write time and never recalculated by MySQL. A SQL REPLACE() operates on bytes with no idea it is inside a serialized structure, so a URL that shrinks from 26 bytes to 19 leaves a prefix claiming 26. unserialize() is strict: byte counts that do not match are a parse error, and it returns false rather than guessing. WordPress interprets that false as “no value” and falls back to a default — which is why corruption from a bad find/replace shows up as missing settings rather than an error page.
wp search-replace closes the gap by doing the work in PHP rather than SQL. For every candidate value it attempts unserialize(); if the value is serialized, it walks the structure, replaces inside each leaf string, and reserializes so every prefix is recomputed from the new content. Values that are not serialized get a straight string replacement. Nested arrays and objects are handled recursively — --recurse-objects controls descending into objects and is on by default, so leave it alone unless you have a specific reason.
Step-by-step
1. Confirm what you are actually replacing. Guessing the old URL is how people end up with half-migrated databases. Ask WordPress:
wp option get siteurl
wp option get home
http://staging.example.com
http://staging.example.com
Match this string exactly — including protocol and any www. http://staging.example.com and staging.example.com are different replacements with different blast radii.
2. Always dry-run first. --dry-run performs the entire pass and reports what it would write, without touching a row. There is no situation where skipping this is correct:
wp search-replace 'http://staging.example.com' 'https://example.com' --dry-run
+---------------+-----------------------+--------------+------+
| Table | Column | Replacements | Type |
+---------------+-----------------------+--------------+------+
| wp_commentmeta| meta_value | 0 | PHP |
| wp_options | option_value | 14 | PHP |
| wp_postmeta | meta_value | 47 | PHP |
| wp_posts | post_content | 218 | SQL |
| wp_posts | guid | 96 | SQL |
| wp_termmeta | meta_value | 0 | PHP |
+---------------+-----------------------+--------------+------+
Success: 377 replacements to be made.
Read the Type column: PHP means that column was processed row-by-row in PHP because it may hold serialized data; SQL means WP-CLI could safely delegate to a SQL query. The dry run ends with “to be made” rather than “Made” — that phrasing is your confirmation nothing was written. To cut the noise on a wide schema, add --report-changed-only and the zero rows disappear.
3. Notice the guid column, and exclude it. That wp_posts.guid line with 96 replacements is a trap. Despite looking like a URL, a post’s GUID is a permanent unique identifier — RSS readers use it to decide whether an item is new. Rewrite GUIDs and every feed reader on the internet may re-flag your entire back catalogue as fresh posts. WordPress itself documents that GUIDs must never change after publication:
wp search-replace 'http://staging.example.com' 'https://example.com' --skip-columns=guid --report-changed-only --dry-run
+---------------+-----------------------+--------------+------+
| Table | Column | Replacements | Type |
+---------------+-----------------------+--------------+------+
| wp_options | option_value | 14 | PHP |
| wp_postmeta | meta_value | 47 | PHP |
| wp_posts | post_content | 218 | SQL |
+---------------+-----------------------+--------------+------+
Success: 279 replacements to be made.
The count dropped by the 96 GUID rows, and --report-changed-only trimmed the empty tables. This is the command you actually want.
4. Run it for real. Drop --dry-run and nothing else changes:
wp search-replace 'http://staging.example.com' 'https://example.com' --skip-columns=guid --report-changed-only
+---------------+-----------------------+--------------+------+
| Table | Column | Replacements | Type |
+---------------+-----------------------+--------------+------+
| wp_options | option_value | 14 | PHP |
| wp_postmeta | meta_value | 47 | PHP |
| wp_posts | post_content | 218 | SQL |
+---------------+-----------------------+--------------+------+
Success: Made 279 replacements.
“Made 279 replacements” — matching your dry-run count — is the success signal. A number that differs from the dry run means something changed the database in between.
5. Reach tables WordPress does not register. By default search-replace only touches tables registered with $wpdb — the core set. Plugins that create their own tables (form entries, order line items, custom log tables) are invisible to that default. --all-tables-with-prefix widens the pass to everything sharing your table prefix, which is almost always the right knob:
wp search-replace 'http://staging.example.com' 'https://example.com' --skip-columns=guid --all-tables-with-prefix --report-changed-only
+---------------------+--------------+--------------+------+
| Table | Column | Replacements | Type |
+---------------------+--------------+--------------+------+
| wp_options | option_value | 14 | PHP |
| wp_postmeta | meta_value | 47 | PHP |
| wp_posts | post_content | 218 | SQL |
| wp_gf_entry_meta | meta_value | 31 | PHP |
+---------------------+--------------+--------------+------+
Success: Made 310 replacements.
--all-tables goes further still — every table in the database regardless of prefix. Reserve it for genuinely shared databases where you know what else lives there, because it will happily rewrite another application’s data.
6. Reach for --precise when a table is skipped or suspicious. WP-CLI optimizes by handing simple columns to a SQL query, which is faster but bypasses PHP. --precise forces every row through the PHP path instead. It is markedly slower on large tables and worth it when you suspect serialized data in an unexpected column, or when a SQL-type row leaves occurrences behind:
wp search-replace 'http://staging.example.com' 'https://example.com' --skip-columns=guid --report-changed-only --precise --dry-run
+---------------+--------------+--------------+------+
| Table | Column | Replacements | Type |
+---------------+--------------+--------------+------+
| wp_options | option_value | 14 | PHP |
| wp_postmeta | meta_value | 47 | PHP |
| wp_posts | post_content | 218 | PHP |
+---------------+--------------+--------------+------+
Success: 279 replacements to be made.
Every row now reads PHP, including post_content, which took the SQL path before. A count that climbs here is the signal that the fast path was missing something.
7. Produce a file instead of writing. --export sends the transformed dump to stdout or a file and leaves the live database untouched — useful when you want the replacement applied on the way into a new environment, or reviewed before it lands:
wp search-replace 'http://staging.example.com' 'https://example.com' --skip-columns=guid --export=migrated.sql
Success: Made 279 replacements and exported to migrated.sql.
8. Flush caches and rewrite permalinks. Your object cache and transients still hold the old URL, and rewrite rules are themselves a serialized option. Neither the diagram nor the database will save you if you stop before this:
wp cache flush
wp rewrite flush
Success: The cache was flushed.
Success: Rewrite rules flushed.
Validate it works
Start where WordPress starts. Both options should now show the new URL:
wp option get siteurl
wp option get home
https://example.com
https://example.com
Then check the raw database for stragglers. --format=count on a fresh dry run gives you a single number, and zero is the answer you want:
wp search-replace 'staging.example.com' 'example.com' --all-tables-with-prefix --dry-run --format=count
0
Note the bare-domain search string here — deliberately looser than the one you replaced with, so it catches protocol-relative //staging.example.com and http/https variants alike. If it returns a non-zero count, the tables it names tell you what the narrower pass missed.
Finish on the front end, logged out, with a hard refresh: load the home page, one post with images, and one page built by your builder. Open the browser devtools network tab and look for requests to the old host — mixed-content warnings and broken images are the usual tell. Confirm the WordPress side with a serialized option that a bad replacement would have destroyed:
wp option get widget_block --format=json
If that returns structured JSON rather than false or an empty value, unserialization survived the pass. On a site still running classic widgets, wp option get widget_text --format=json makes the same point — any serialized, URL-bearing option will do.
Troubleshooting
- You forgot
--dry-runand want it back. There is no rollback in the command. Restore the dump:wp db import ~/backup-pre-replace.sql. If you skipped the backup, your options are your host’s nightly snapshot or reversing the replacement —wp search-replace 'https://example.com' 'http://staging.example.com'— which works only if the strings are unambiguous and nothing else wrote in between. Treat a successful reverse as luck, not procedure. - GUIDs were rewritten. If you already ran without
--skip-columns=guid, restore from backup. Reversing only the GUID column is possible in principle but you cannot distinguish GUIDs that legitimately contained the old domain from those that did not. The cost of getting this wrong is every subscriber’s feed reader treating your archive as new posts. - Occurrences remain after a clean run. Three usual causes. First, plugin tables outside
$wpdb— rerun with--all-tables-with-prefix. Second, caches: transients inwp_options, an object cache in Redis or Memcached, and a page cache in front all hold pre-replacement copies, sowp cache flushand a purge at the CDN or reverse proxy. Third, a column that took theSQLfast path — rerun that pass with--precise. httpversushttps, and protocol-relative leftovers. Replacinghttp://old.example.comnever matcheshttps://old.example.comor//old.example.com. Do the protocol-bearing pass first, then a second narrow pass on the bare host, checking the dry run each time:wp search-replace '//staging.example.com' '//example.com' --dry-run. Never replace a bareexample.comwith no delimiter — it will hit substrings inside email addresses and unrelated text.- Multisite replaced nothing, or only one site. On a network, a plain
wp search-replaceerrors or silently targets the main site. Select the site with--urland add--networkto include network-wide tables:wp search-replace 'http://staging.example.com' 'https://example.com' --url=staging.example.com --network --skip-columns=guid --dry-run. Thewp_blogsandwp_sitedomain columns are separate bookkeeping — verify them withwp site list --fields=blog_id,urlafterwards and fix withwp site updaterather than a text pass. - The URL is hardcoded in theme or plugin files. A database pass cannot reach PHP, JS, or CSS on disk, and neither can it fix a
WP_HOMEorWP_SITEURLconstant pinned inwp-config.php. Grep the tree —grep -rn 'staging.example.com' wp-content/themes wp-content/plugins wp-config.php— and fix those by hand. Ifwp option get siteurldisagrees with the front end, a constant is overriding the database.
When this is the wrong approach
wp search-replace is the correct tool for a URL change inside a WordPress database, and it is the wrong tool for several adjacent jobs. It is not a general-purpose database editor: for structured changes, wp option update and wp post meta update say what they mean and cannot corrupt a neighbouring row. Regex mode (--regex) does still unserialize correctly — the pattern runs against each leaf string before the value is reserialized — but it costs roughly 15-20x the runtime, and it skips the second pass a literal search makes over JSON-escaped copies of your search string, so occurrences stored as http:\/\/staging.example.com inside block markup survive it. The risk also moves into your pattern, where a greedy expression matches more than you meant. Use a literal search whenever a literal search will do. On a very large database the PHP path is slow and takes locks; run it in a maintenance window, or produce a file with --export and load it during the cutover instead of transforming production in place. And if the URL never needed to change — a site that already serves the right domain but renders the wrong one — you have a cache or a wp-config.php constant, not a database problem, and a replacement pass will simply report zero. For the wider command set this builds on, see advanced WP-CLI commands, and for where this step fits a full move, migrating WordPress from a generic host. The flag reference lives in the WP-CLI search-replace docs, and the format that makes all of this necessary is documented at PHP’s serialize().
Running WordPress that earns real money deserves an operator — Stack Harbor runs that stack under enterprise WordPress operations; book an infrastructure consult.