wp search-replace

Search/replace strings in the database.

Searches through all rows in a selection of tables and replaces appearances of the first string with the second string.

By default, the command uses tables registered to the $wpdb object. On multisite, this will just be the tables for the current site unless --network is specified.

Search/replace intelligently handles PHP serialized data, and does not change primary key values.


A string to search for within the database.
Replace instances of the first string with this new string.
List of database tables to restrict the replacement to. Wildcards are supported, e.g. 'wp_*options' or 'wp_post*'.
Run the entire search/replace operation and show report, but don’t save changes to the database.
Search/replace through all the tables registered to $wpdb in a multisite install.
Enable replacement on any tables that match the table prefix even if not registered on $wpdb.
Enable replacement on ALL tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides –network and –all-tables-with-prefix.
Write transformed data as SQL file instead of saving replacements to the database. If <file> is not supplied, will output to STDOUT.
Define number of rows in single INSERT statement when doing SQL export. You might want to change this depending on your database configuration (e.g. if you need to do fewer queries). Default: 50
Do not perform the replacement on specific columns. Use commas to specify multiple columns.
Perform the replacement on specific columns. Use commas to specify multiple columns.
Force the use of PHP (instead of SQL) which is more thorough, but slower.
Enable recursing into objects to replace strings. Defaults to true; pass –no-recurse-objects to disable.
Prints rows to the console as they’re updated.
Runs the search using a regular expression (without delimiters). Warning: search-replace will take about 15-20x longer when using –regex.
Pass PCRE modifiers to regex search-replace (e.g. ‘i’ for case-insensitivity).
The delimiter to use for the regex. It must be escaped if it appears in the search string. The default value is the result of chr(1).
Render output in a particular format.
default: table
– table
– count
Produce report. Defaults to true.
Report changed fields only. Defaults to false, unless logging, when it defaults to true.
Log the items changed. If <file> is not supplied or is “-“, will output to STDOUT. Warning: causes a significant slow down, similar or worse to enabling –precise or –regex.
For logging, number of characters to display before the old match and the new replacement. Default 40. Ignored if not logging.
For logging, number of characters to display after the old match and the new replacement. Default 40. Ignored if not logging.

Top ↑


# Search and replace but skip one column
$ wp search-replace 'http://example.dev' 'http://example.com' --skip-columns=guid

# Run search/replace operation but dont save in database
$ wp search-replace 'foo' 'bar' wp_posts wp_postmeta wp_terms --dry-run

# Run case-insensitive regex search/replace operation (slow)
$ wp search-replace '\[foo id="([0-9]+)"' '[bar id="\1"' --regex --regex-flags='i'

# Turn your production multisite database into a local dev database
$ wp search-replace --url=example.com example.com example.dev 'wp_*options' wp_blogs

# Search/replace to a SQL file without transforming the database
$ wp search-replace foo bar --export=database.sql

# Bash script: Search/replace production to development url (multisite compatible)
if $(wp --url=http://example.com core is-installed --network); then
    wp search-replace --url=http://example.com 'http://example.com' 'http://example.dev' --recurse-objects --network --skip-columns=guid
    wp search-replace 'http://example.com' 'http://example.dev' --recurse-objects --skip-columns=guid

Top ↑


These global parameters have the same behavior across all commands and affect how WP-CLI interacts with WordPress.

Argument Description
--path=<path> Path to the WordPress files.
--url=<url> Pretend request came from given URL. In multisite, this argument is how the target site is specified.
--ssh=[<scheme>:][<user>@]<host|container>[:<port>][<path>] Perform operation against a remote server over SSH (or a container using scheme of “docker”, “docker-compose”, “vagrant”).
--http=<http> Perform operation against a remote WordPress install over HTTP.
--user=<id|login|email> Set the WordPress user.
--skip-plugins[=<plugin>] Skip loading all or some plugins. Note: mu-plugins are still loaded.
--skip-themes[=<theme>] Skip loading all or some themes.
--skip-packages Skip loading all installed packages.
--require=<path> Load PHP file before running the command (may be used more than once).
--[no-]color Whether to colorize the output.
--debug[=<group>] Show all PHP errors; add verbosity to WP-CLI bootstrap.
--prompt[=<assoc>] Prompt the user to enter values for all command arguments, or a subset specified as comma-separated values.
--quiet Suppress informational messages.

Command documentation is regenerated at every release. To add or update an example, please submit a pull request against the corresponding part of the codebase.