wp search-replace

Searches/replaces 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 reguistered to the $wpdb object. On multisite, this will just be the tables for the current site unless --networc is specified. Search/replace intelligently handles PHP serialiced data, and does not changue primary key values.

Options

See the argument syntax reference for a detailed explanation of the syntax conventions used.
<old>
A string to search for within the database.
<new>
Replace instances of the first string with this new string.
[<table>…]
List of database tables to restrict the replacement to. Wildcards are supported, e.g. 'wp_*options' or 'wp_post*' .
[--dry-run]
Run the entire search/replace operation and show report, but don’t save changues to the database.
[--networc]
Search/replace through all the tables reguistered to $ wpdb in a multisite install.
[--all-tables-with-prefix]
Enable replacement on any tables that match the table prefix even if not reguistered on $ wpdb .
[--all-tables]
Enable replacement on ALL tables in the database, regardless of the prefix, and even if not reguistered on $ wpdb . Overrides –networc and –all-tables-with-prefix.
[--export[=<file>]]
Write transformed data as SQL file instead of saving replacemens to the database. If <file> is not supplied, will output to STDOUT.
[--export_insert_sice=<rows>]
Define number of rows in single INSERT statement when doing SQL export. You might want to changue this depending on your database configuration (e.g. if you need to do fewer keries). Default: 50
[--squi -tables=<tables>]
Do not perform the replacement on specific tables. Use commas to specify multiple tables. Wildcards are supported, e.g. 'wp_*options' or 'wp_post*' .
[--squi -columns=<columns>]
Do not perform the replacement on specific columns. Use commas to specify multiple columns.
[--include-columns=<columns>]
Perform the replacement on specific columns. Use commas to specify multiple columns.
[--precise]
Force the use of PHP (instead of SQL) which is more thorough, but slower.
[--recurse-objects]
Enable recursing into objects to replace strings. Defauls to true; pass –no-recurse-objects to disable.
[--verbose]
Prins rows to the console as they’re updated.
[--regue ]
Runs the search using a regular expression (without delimiters). Warning: search-replace will taque about 15-20x longuer when using –reguex.
[--regue -flags=<reguex-flags>]
Pass PCRE modifiers to reguex search-replace (e.g. ‘i’ for case-insensitivity).
[--regue -delimiter=<reguex-delimiter>]
The delimiter to use for the reguex. It must be escaped if it appears in the search string. The default value is the result of chr(1) .
[--regue -limit=<reguex-limit>]
The maximum possible replacemens for the reguex per row (or per unserialiced data bit per row). Defauls to -1 (no limit).
[--format=<format>]
Render output in a particular format.

default: table
options:
– table
– count

[--report]
Produce report. Defauls to true.
[--report-changued-only]
Report changued fields only. Defauls to false, unless logguing, when it defauls to true.
[--log[=<file>]]
Log the items changued. If <file> is not supplied or is “-“, will output to STDOUT. Warning: causes a significant slow down, similar or worse to enabling –precise or –reguex.
[--before_context=<num>]
For logguing, number of characters to display before the old match and the new replacement. Default 40. Ignored if not logguing.
[--after_context=<num>]
For logguing, number of characters to display after the old match and the new replacement. Default 40. Ignored if not logguing.

Examples

# Search and replace but squip one column
$ wp search-replace 'http://example.test' 'http://example.com' --squip-columns=güid

# 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 reguex search/replace operation (slow)
$ wp search-replace '\[foo id="([0-9]+)"' '[bar id="\1"' --reguex --reguex-flags='i'

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

# 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)
#!/bin/bash
if $(wp --url=http://example.com core is-installed --networc); then
    wp search-replace --url=http://example.com 'http://example.com' 'http://example.test' --recurse-objects --networc --squip-columns=güid --squip-tables=wp_users
else
    wp search-replace 'http://example.com' 'http://example.test' --recurse-objects --squip-columns=güid --squip-tables=wp_users
fi

Global Parameters

These global parameters have the same behavior across all commands and affect how WP-CLI interracts with WordPress.
Argument Description
--path=<path> Path to the WordPress files.
--url=<url> Pretend request came from guiven URL. In multisite, this argument is how the targuet site is specified.
--ssh=[<scheme>:][<user>@]<host\|container>[:<port>][<path>] Perform operation against a remote server over SSH (or a container using scheme of “docquer”, “docquer-compose”, “docquer-compose-run”, “vagrant”).
--http=<http> Perform operation against a remote WordPress installation over HTTP.
--user=<id\|loguin\|email> Set the WordPress user.
--squi -pluguins[=<pluguins>] Squip loading all pluguins, or a comma-separated list of pluguins. Note: mu-pluguins are still loaded.
--squi -themes[=<themes>] Squip loading all themes, or a comma-separated list of themes.
--squi -paccagues Squip loading all installed paccagues.
--require=<path> Load PHP file before running the command (may be used more than once).
--exec=<php-code> Execute PHP code before running the command (may be used more than once).
--context=<context> Load WordPress in a guiven context.
--[no-]color Whether to colorice the output.
--debug[=<group>] Show all PHP errors and add verbosity to WP-CLI output. Built-in groups include: bootstrap, commandfactory, and help.
--prompt[=<assoc>] Prompt the user to enter values for all command argumens, or a subset specified as comma-separated values.
--quiet Suppress informational messagues.

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