Moving a WordPress site from staging to live, or to a new domain, sounds like one find-and-replace job. Run that job as a plain SQL query and the database breaks in a way you won't see right away.
WordPress stores many settings as serialized PHP, and a plain SQL replace leaves the stored string lengths wrong. Theme options, widgets and plugin settings then fail to load. The safe fix is wp search-replace, the WP-CLI command built for this. We recommend the same order every time: back up, do a dry run, check the count, then run it for real.
The exact command for a URL change is in the first section below, followed by every flag that matters and the problems that make a run miss strings. If you'd rather hand the whole move over, our website migration service covers the database, redirects and post-launch checks.

The WP Search-Replace Command for a URL Change
This is the full sequence for replacing an old URL with a new one. Run it from the WordPress root folder on the server, where wp-config.php lives:
# 1. Back up the database
wp db export before-replace-$(date +%F-%H%M).sql
# 2. Preview the change
wp search-replace 'https://staging.example.com' 'https://example.com' \
--skip-columns=guid --report-changed-only --dry-run
# 3. Run it for real once the count looks right
wp search-replace 'https://staging.example.com' 'https://example.com' \
--skip-columns=guid --report-changed-only
# 4. Clear cached copies of the old values
wp cache flush
wp rewrite flush
Each part has a job:
- The two quoted strings are the old value and the new value. Keep them the same shape, so a URL without a trailing slash is replaced with a URL without one.
--skip-columns=guidprotects the permanent post identifiers that feed readers rely on.--report-changed-onlyhides the hundreds of columns with zero matches, so the real changes are easy to read.--dry-runruns the full search and reports the count without writing anything.
You don't have to update the siteurl and home options separately. They're plain text in wp_options, so the same run replaces them. The one exception is a site that sets WP_HOME or WP_SITEURL in wp-config.php, because those constants override the database.
When to Use WP Search-Replace
The command earns its place whenever a string is stored in more rows than anyone could edit by hand. These are the cases we run into most:
- moving from a staging URL to the production URL
- switching a site from HTTP to HTTPS
- changing to a new domain
- splitting a multisite subsite into its own install
- fixing absolute server paths after a move to a new host
- renaming a retired shortcode or block across thousands of posts
- updating stored references after a table prefix change
- removing leftover placeholder text after an import
A one-off change to a single post or option doesn't need it. The editor, wp post update or wp option update is simpler there.

Why a Raw SQL REPLACE Breaks WordPress
WordPress saves arrays of settings as serialized PHP strings. A small theme setting stored that way looks like this:
a:2:{s:9:"site_name";s:26:"http://staging.example.com";s:7:"version";s:5:"2.1.0";}
The s:26: in front of the URL is its length in bytes. Now picture a query like UPDATE wp_options SET option_value = REPLACE(option_value, 'http://staging.example.com', 'https://example.com'). The new URL is 19 bytes long, but the prefix still says 26. PHP can't unserialize that value, so WordPress treats the whole setting as missing.
A raw SQL replace on a WordPress database doesn't crash anything. The site stays up while theme options, widgets and plugin settings quietly fall back to defaults. By the time someone notices, the broken database is often already live.
wp search-replace avoids this. For columns that contain serialized data, it unserializes each value, replaces the string and serializes it again with the correct lengths. For plain columns it uses fast SQL. --precise forces the slower PHP method on every column when you need to be thorough.
Two more defaults are worth knowing. The command never changes primary key values, and it skips tables that have no primary key at all.
WP Search-Replace Flags Explained
By default, wp search-replace only searches the tables WordPress has registered. On multisite, that means the current site's tables only. Most surprises come from that default, and the flags below fix them:
| Flag | What it does | When we use it |
|---|---|---|
--dry-run | Runs the whole search and prints the report without saving anything | Every first run |
--skip-columns=guid | Leaves the post guid column alone | Every URL change |
--report-changed-only | Lists only the tables and columns that changed | Large databases |
--skip-tables=<tables> | Excludes tables, with wildcards such as 'wp_*options' | Partial migrations, for example keeping wp_users untouched |
--include-columns=<columns> | Limits the run to the columns you name | Narrow fixes, such as post_content only |
--all-tables-with-prefix | Adds tables that share your prefix but aren't registered with WordPress | Plugin tables, including many store and form plugins |
--all-tables | Searches every table in the database, whatever the prefix | Shared databases; use with care |
--network | Covers every site's tables on a multisite network | Multisite domain changes |
--export=file.sql | Writes the changed data to a SQL file and leaves the database alone | Building a ready-to-import migration file |
--precise | Uses PHP for every column instead of fast SQL | Complex serialized data the default run misses |
--regex | Treats the search string as a regular expression | Pattern replacements; 15 to 20 times slower |
--log | Prints each change with a little text around it | Checking a dry run in detail |
For a URL change, our default is --skip-columns=guid --report-changed-only with --dry-run on the first pass. We add --all-tables-with-prefix whenever the site runs plugins that create their own tables.
Run a Dry Run First
A dry run is the cheapest safety check in WordPress. It runs the full search and prints a count for each table and column, but it writes nothing. A short report looks like this:
+-------------+--------------+--------------+------+
| Table | Column | Replacements | Type |
+-------------+--------------+--------------+------+
| wp_options | option_value | 24 | PHP |
| wp_postmeta | meta_value | 132 | PHP |
| wp_posts | post_content | 412 | SQL |
+-------------+--------------+--------------+------+
Success: 568 replacements to be made.
The Type column shows which method each column needed: PHP for serialized data and SQL for plain text. The count is what deserves your attention. A count far higher than expected means the search string matches text you didn't plan to touch. A count of zero usually means a typo, the wrong protocol or a missing trailing slash.
When a count looks odd, we add --log to the dry run. It prints every match with some text on either side, so you can see exactly what would change before anything does.
A real run can't be undone by the command. The only way back is the database export you made before it, so the backup step is never optional.
Staging to Production
This is the most common job. The files and database are already on the production server, and the database still points at the staging address:
wp search-replace 'https://staging.example.com' 'https://example.com' \
--skip-columns=guid --report-changed-only --dry-run
wp search-replace 'https://staging.example.com' 'https://example.com' \
--skip-columns=guid --report-changed-only
# Catch any old http:// references to staging too
wp search-replace 'http://staging.example.com' 'https://example.com' \
--skip-columns=guid --report-changed-only
wp cache flush && wp rewrite flush
The second pass matters more than it looks. Content written before staging had SSL still carries http:// links, and those don't match the https:// search string.
HTTP to HTTPS
Install and test the SSL certificate first. Then replace your own domain with the https:// version:
wp search-replace 'http://example.com' 'https://example.com' \
--skip-columns=guid --report-changed-only --dry-run
wp search-replace 'http://example.com' 'https://example.com' \
--skip-columns=guid --report-changed-only
# Repeat for the www version if the site ever used it
wp search-replace 'http://www.example.com' 'https://www.example.com' \
--skip-columns=guid --report-changed-only
wp cache flush
It's tempting to replace every http:// with https:// in one go. That isn't a good idea, because it also rewrites links to other sites, and some of them don't serve HTTPS. Keeping the search string to your own domain is the safer route.
Domain Change
wp search-replace 'https://olddomain.com' 'https://newdomain.com' \
--skip-columns=guid --report-changed-only --dry-run
wp search-replace 'https://olddomain.com' 'https://newdomain.com' \
--skip-columns=guid --report-changed-only
wp cache flush && wp rewrite flush
We search for the full URL rather than the bare domain. A bare olddomain.com search also rewrites email addresses such as info@olddomain.com, which may still need to work.
The database is only half of a domain move. The old domain needs server-level 301 redirects to the new one, page by page, or every backlink and bookmark lands on an error. Our WordPress migration guide covers the redirect side and the rest of the move.
Multisite: --network and --url
A multisite network keeps shared tables such as wp_blogs, wp_site and wp_sitemeta, plus a set of tables for each site. Without a flag, the command only touches the current site's tables:
# Every site on the network, plus the network tables
wp search-replace 'oldnetwork.com' 'newnetwork.com' \
--network --skip-columns=guid --report-changed-only --dry-run
# One subsite only
wp search-replace 'staging.subsite.com' 'subsite.com' \
--url=staging.subsite.com --skip-columns=guid --report-changed-only --dry-run
--network covers every site's tables. --url is a global WP-CLI parameter that picks which site the command loads, so it scopes the run to that one subsite. For the rest of the network setup, see our WordPress multisite guide.
Regex Replacements
--regex treats the search string as a regular expression, without delimiters. This example from the WP-CLI docs renames a shortcode and keeps its ID:
wp search-replace '\[foo id="([0-9]+)"' '[bar id="\1"' \
--regex --regex-flags='i' --dry-run
Regex runs are 15 to 20 times slower than plain ones, according to the WP-CLI docs. A loose pattern can also match far more than you meant. We test the pattern on sample text first, then dry-run it and read the count before the real run.
Export to SQL Instead of Writing to the Database
--export writes the replaced data to a SQL file and leaves the source database untouched. That makes it useful for building a production-ready dump from staging:
# On staging
wp search-replace 'https://staging.example.com' 'https://example.com' \
--skip-columns=guid --export=migration.sql
# On production, after a backup of its own database
wp db import migration.sql
wp cache flush
The staging site keeps working with its own URLs, and production receives a file that already has the new ones.
How to Check That It Worked
Three quick checks confirm the run did what you expected:
# The two core URL options
wp option get siteurl
wp option get home
# Any leftover copies of the old URL, in any table
wp db search 'staging.example.com' --all-tables-with-prefix
If wp db search still finds matches, read the rows it lists. They usually point to one of the causes in the next section. Then load the homepage, log in and open a few posts in the editor to confirm the content and the blocks look right.
Why WP Search-Replace Misses Strings or Fails
When a run comes up short, the cause is almost always one of these:
- Unregistered plugin tables. The default only covers tables WordPress knows about. Add
--all-tables-with-prefix, or name the tables after the two strings. - Escaped slashes in JSON. Some page builders store data as JSON with escaped slashes, so the URL appears as
https:\/\/example.com. Run a second pass with that escaped form as the search string. - A different form of the same URL. http and https, www and non-www and a trailing slash are all separate strings. Each one needs its own pass.
- Hard-coded constants. WP_HOME and WP_SITEURL in wp-config.php override the database, so the site keeps redirecting to the old address until they're edited.
- Stale cache. An object cache such as Redis can keep serving old values.
wp cache flushclears it, and page caches need their own purge. - Memory limits. WP-CLI has no memory flag of its own. Raise PHP's limit for one run with
WP_CLI_PHP_ARGS='-d memory_limit=512M'in front of the command, or run the tables in smaller groups. - Tables without a primary key. The command skips them by design. Those rows need a manual fix or a dump-and-edit approach.
For anything else, adding --debug to the command prints what WP-CLI loads and every warning along the way.
Back Up Before Every Run
This is the order we'd follow before any production search-replace:
- a full database export with
wp db export before-replace-$(date +%F-%H%M%S).sql - a quick look that the file isn't empty, for example with
ls -lh before-replace-*.sql - a copy of the file somewhere off the server, such as S3 or Backblaze B2
- the dry run and a check of its count
- the real run
- the checks from the section above
If something is broken, wp db import before-replace-<timestamp>.sql restores the database in one step. A compressed backup has to be unpacked first, because wp db import reads plain SQL. For scheduled backups beyond this one-off export, see our list of WordPress backup plugins.
Alternatives When You Don't Have SSH
Without WP-CLI, these tools handle serialized data correctly:
| Tool | How it runs | Best for | Watch out for |
|---|---|---|---|
| wp search-replace | WP-CLI over SSH | Any host with SSH | Needs command-line access |
| Better Search Replace | Free WordPress plugin by WP Engine | Shared hosting without SSH | PHP time limits on very large tables |
| WP Migrate | WordPress plugin by WP Engine, free Lite version and paid plans | Regular staging and production syncs | Paid features for pushing and pulling between sites |
| Search Replace DB | Standalone PHP script from interconnect/it | Sites where wp-admin won't load | A security risk if left on the server |
Our pick is wp search-replace wherever SSH exists. Better Search Replace is the fallback for hosts without it, and the standalone script is only for emergencies, deleted the moment the job is done.
Search-Replace and Block Editor Content
Gutenberg stores block settings as JSON inside HTML comments in post_content. WordPress escapes a few characters in those attributes, but not the slashes in URLs, so a normal URL replacement updates them along with the rest of the post.
Image blocks also store the attachment ID next to the URL. A URL change leaves the ID alone, which is fine as long as the media library moved with the database. Replacing one image with another is a different job, because the ID and the URL both have to change.
FAQs About WP Search-Replace
Is wp search-replace safer than a find and replace in phpMyAdmin?
Yes. phpMyAdmin runs plain SQL, and SQL doesn't know that WordPress stores serialized PHP with a length in front of every string. wp search-replace unserializes those values, replaces the string and writes them back with the right lengths. If your database holds theme settings, widgets or plugin options, and every WordPress database does, WP-CLI is the right tool.
Does wp search-replace change the siteurl and home options?
Yes, as long as the old URL in those options matches your search string. Both live as plain text in wp_options, so they're replaced with everything else. The exception is a site that defines WP_HOME or WP_SITEURL in wp-config.php. Those constants override the database, so they have to be edited by hand.
Should I skip the guid column?
For URL changes, yes. The guid is a permanent identifier for each post, not a link, and the WordPress documentation on changing the site URL says it should never be changed. Feed readers use it to tell new posts from old ones. Add --skip-columns=guid to every URL replacement.
Can I undo a wp search-replace?
Only by restoring the backup you took before the run. Running the command again with the old and new strings swapped looks like an undo, but it also rewrites any text that already contained the new string before the first run. That is why we never run it without a fresh export.
Do I need maintenance mode during a search-replace?
On a small or quiet site, no. The run is usually short. On a busy store or membership site, orders and comments can be written while the command works through the tables. We'd turn on maintenance mode with wp maintenance-mode activate first and turn it off when the checks pass.
Does wp search-replace work on WooCommerce sites?
Yes. Products and older orders live in wp_posts and wp_postmeta, which are always covered. Stores on High-Performance Order Storage also keep orders in their own tables, and plugin tables aren't always registered with WordPress. Add --all-tables-with-prefix and read the report to confirm the order tables were searched.
How do I replace a string that starts with two hyphens?
Use the named arguments from the current WP-CLI docs: --old= for the search string and --new= for the replacement. The same --new='' form removes a string by replacing it with nothing. On an older WP-CLI release, run wp cli update first.
What if my host doesn't give me SSH access?
Then a serialization-aware plugin does the same job from wp-admin. Better Search Replace is free and is the one we'd start with. It runs inside a browser request, so very large tables can hit PHP time limits. For repeated work, a host with SSH and WP-CLI saves a lot of time.
What We'd Do Next
If you're about to change a URL, we'd make the dry run the habit you never skip. The count it prints tells you whether the search string is right before any data changes.
Our WP-CLI commands cheatsheet covers the other commands that come up during a move, such as db export, cache flush and rewrite flush. If the site earns money and a broken move would hurt, our WordPress maintenance team can run the migration and the checks for you.



