Editorial illustration.
Moving a WordPress site from a staging domain to production can leave the old address in widgets, page-builder settings and other database values. A blind SQL replacement is risky because some PHP data is serialized: changing a string without updating its stored length can corrupt the value. WP-CLI’s search-replace understands serialized data and offers a dry run, but it still needs a scoped, reversible workflow.
Summary
- Back up the database and files, and run the command against the intended installation.
- Start with
--dry-run; inspect the proposed scope before writing changes. - Use exact old and new URLs, select tables deliberately, and add
--networkonly when the entire multisite network is intended. - After the replacement, check canonical URLs, media, redirects, login, forms and serialized settings on staging before switching traffic.
Know what the command changes
wp search-replace searches registered database tables by default and replaces matching strings. WP-CLI documents that it handles PHP serialized data and leaves primary key values unchanged. It also supports --dry-run, table selection, network scope and SQL export options (official command reference). This makes it a practical migration tool, not a substitute for a backup or a review of what the old string means.
For example, https://staging.example.test and https://example.com are different values from http://staging.example.test. Decide whether the move changes the scheme, hostname, path prefix or all three. Avoid a broad replacement like staging to production: that could alter unrelated text, usernames or content.
WP-CLI is a command-line tool. Run it from the WordPress installation directory or specify --path=/path/to/wordpress. Confirm first that the shell points to the database you mean to change. On a host with production and staging installations, check wp option get home and wp option get siteurl before proceeding.
Prepare a reversible migration
- Put the site into a maintenance window if users could submit content while you copy or change data.
- Make a database backup and a separate copy of uploaded files. Store them outside the directory that will be overwritten.
- Restore the backup to staging or a local copy and confirm that copy loads.
- Check the current WordPress path and URLs. For example, run
wp --path=/srv/www/site option get homeandwp --path=/srv/www/site option get siteurl. - Decide the exact old and new strings, including
httpversushttps,www, and any subdirectory.
Do not skip the restore check. A backup that has never been opened is only a hope. Our WordPress backup restore rehearsal explains how to test the recovery path before a change.
Run a dry run first
On the staging copy, preview a narrow replacement:
wp --path=/srv/www/site search-replace \
'https://staging.example.test' \
'https://example.com' \
--dry-run
Read the output for table names and proposed changes. If the results include unexpected tables, stop and investigate before dropping --dry-run. Specify tables when the change should be limited, for example wp_options wp_posts wp_postmeta. Do not assume every site’s table prefix is wp_; WP-CLI usually discovers the site’s registered table names, but custom plugins and legacy schemas can add their own.
If your database stores URLs without a protocol, run a separate preview for that exact variant only when you have evidence it exists. Likewise, a site moving from a subdirectory may need more than a hostname replacement. Keep each operation small enough that its output is understandable.
Apply only the reviewed change
After the dry run is correct, repeat the same command without --dry-run on staging. The tool’s PHP handling of serialized values avoids a common failure of raw SQL string replacement. Do not add --precise automatically: the WP-CLI documentation says it forces PHP processing across all columns, which can be slower. Use it only when the standard command has a demonstrated data-handling issue and you understand the runtime cost.
For a multisite network, the default scope is the current site’s tables. --network broadens the operation across registered sites in the network, so use it only if the domain is changing for all of them. Pass the correct --url for a specific site when operating in a multisite context, and verify with a dry run first. --all-tables is broader still; it can touch tables that do not belong to WordPress, so avoid it unless the database inventory is understood.
One possible staging command is:
wp --path=/srv/www/site search-replace \
'https://staging.example.test' \
'https://example.com' \
wp_options wp_posts wp_postmeta
Your table names and migration scope may differ. Treat this as a pattern, not a command to paste blindly into production.
Verify the site after replacement
Open the staging site and check the front page, several articles, images, menus, admin login and any checkout or contact flow. Look for mixed-content warnings, broken media and links that still point to staging. Check the home and siteurl options, then inspect canonical URLs and sitemap output. If a cache or CDN is in use, purge it only after the origin responds correctly.
Also check plugin-specific settings. Some plugins store an address in a custom format or outside the tables you selected. A successful command is not proof that every integration has migrated. Run a targeted search for the old domain in exported database text or use a staging crawler, while avoiding unnecessary publication of private data.
When the site passes review, repeat the operation on production only during the planned change window and with a fresh backup. Recheck a representative set of pages and forms immediately afterward. Keep the old site or rollback path available until DNS, certificates, redirects and third-party callbacks have been confirmed. Our WordPress URL-change checklist covers the surrounding cutover steps.
Mistakes to avoid
- Skipping the dry run: you lose the chance to spot a wrong database or wide table scope.
- Using an imprecise search value: replacing a bare hostname or word can change unrelated content.
- Treating multisite as one site: one site’s tables are not automatically the entire network.
- Using SQL
REPLACE()on serialized values: stored string lengths can become invalid. - Ignoring external dependencies: DNS, SSL, CDN, email callbacks and payment webhooks may need their own updates.
- Deleting the backup too soon: keep a known recovery copy until the new domain has been checked in real use.
My Take
The safest part of WP-CLI search-replace is not a special flag; it is the discipline around the command. A known path, an exact URL, a dry run and a verified restore turn a potentially broad database edit into a controlled migration. If the preview is surprising, pause there. It is cheaper to investigate a table than to repair a damaged production database.
Conclusion
Use WP-CLI for the serialized-data awareness and the visibility of its dry run, but keep the operation narrow. Verify the installation, back up and restore-test, preview exact strings, apply on staging, and test site behavior before the production cutover. For a complex multisite or custom database, document the intended table scope before executing anything.