Command builder

Generate a safe wp search-replace command

Moving WordPress to a new domain, or from http to https? Fill in what to replace and copy a command sequence that backs up first and previews every change with --dry-run before anything is written.

What to replace

→
Flags

Warning: this edits your database directly. wp search-replace rewrites your content in place, in every table it touches, and there is no undo. Only run it if you're sure what it will change, and make sure you have a backup you can restore. Step 1 below creates one; if something goes wrong, wp db import before-search-replace.sql puts it back.

Run it safely, in this order

  1. Back up the database

     
  2. Preview the changes (nothing is written)

     
  3. Run it for real

     
  4. Clear the cache

     

Read the preview's counts before step 3. If a number looks wrong, fix the search value and preview again.

Common wp search-replace recipes

Swap in your own domains. Each one starts as a dry run: when the counts look right, run it again without --dry-run.

All of this on one printable page: WP-CLI search-replace cheat sheet (PDF, A4).

Move a site to a new domain

The classic migration. Including https:// keeps email addresses like info@old.example untouched. If the old site also has http:// links, run it once more for those.

wp search-replace 'https://old.example' 'https://new.example' --skip-columns=guid --dry-run

Switch from http to https

After installing a certificate, rewrite every hard-coded http:// link to your own domain.

wp search-replace 'http://example.com' 'https://example.com' --skip-columns=guid --dry-run

Staging to live, including plugin tables

Covers every table with your WordPress prefix, including plugin tables WordPress doesn't register.

wp search-replace 'staging.example.com' 'example.com' --all-tables-with-prefix --skip-columns=guid --dry-run

Every site in a multisite network

Runs through the tables of every site in the network. To change a single site instead, drop --network and add --url=site.example.com.

wp search-replace 'old.example' 'new.example' --network --skip-columns=guid --dry-run

Export an SQL file instead of changing the database

Writes the replaced data to a file you can import elsewhere. The live database stays as it is.

wp search-replace 'old.example' 'new.example' --export=migrated.sql

What the options do

--dry-run
Reports how many replacements would happen, without changing the database. That's step 2 above, so the preview always has it.
Which tables
Registered tables (default) covers WordPress's own tables — right for most domain moves. With the WP prefix also includes custom/plugin tables that share your prefix. All tables touches every table in the database regardless of prefix — only when you know why (e.g. multiple installs in one DB). Specific tables lets you name exact tables. The prefix itself is read from wp-config.php automatically, so there's no prefix to type.
--skip-columns
Columns to leave untouched. guid is the classic one — a post's GUID is a permanent identifier and must never be rewritten, even during a domain move.
--recurse-objects
WordPress stores serialized PHP arrays/objects (widget and plugin settings) in the database; this replaces safely inside them instead of corrupting the length-prefixed data. WP-CLI enables it by default — only tick the box if you want the flag stated explicitly.
--precise
Forces PHP (instead of the faster MySQL path) for replacement — more thorough with serialized data, but slower on big databases.
--network
Multisite only. Runs across every site in the network (wp_2_*, wp_3_*, …) rather than just the current site. The Multisite URL field instead targets one specific site.
--allow-root
WP-CLI refuses to run as the Unix root user by default (a safety guard). This overrides it — handy when you're SSH'd in as root.

Common WordPress tables: wp_posts, wp_postmeta, wp_options, wp_users, wp_usermeta, wp_comments, wp_commentmeta, wp_terms, wp_term_taxonomy, wp_term_relationships.

Questions about wp search-replace

Does wp search-replace handle serialized data?

Yes. WordPress stores many settings (widgets, theme and plugin options) as serialized PHP, where every string carries its own length. WP-CLI unpacks those values, replaces inside them and packs them again with the right lengths. A plain SQL REPLACE() would corrupt them. --precise makes it use this careful PHP path for every column: slower, but thorough.

What does --dry-run show?

It runs the whole operation and prints a table of every table and column with the number of replacements it would make, but saves nothing. Read the counts, then run the same command without --dry-run.

Should I skip the guid column?

On a live site, yes. WordPress's own guide to moving a site says never to change the GUID: feed readers use it to recognise posts, and a changed GUID makes old posts show up as new. --skip-columns=guid leaves it alone, which is why the form fills it in.

What's the difference between --all-tables-with-prefix and --all-tables?

--all-tables-with-prefix adds every table that starts with your WordPress prefix (usually wp_), including plugin tables WordPress doesn't register. --all-tables takes every table in the database, whatever its name. Only use it when the database holds this one site.

How do I run it on a multisite network?

Add --network to search every site's tables. To target one site, leave --network off and pass --url=site.example.com; in a multisite, that's how WP-CLI picks the site.

Why does it report 0 replacements?

The search value has to match exactly, including http or https and any trailing slash. In a multisite, check you're on the right site (--url). If the text lives in a plugin's own table, add --all-tables-with-prefix.

Can I do this without the command line?

Yes: plugins such as Better Search Replace do the same from the WordPress admin. WP-CLI is faster on large databases and doesn't run into PHP time limits.

Sources & references