wp search-replace
Searches/replace strings in the database.
Searches through all rows in a selection of tables and replace 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.
See also commands for working with DB wp db.
Parameters
You can specify Global parameters and the following:
- {old}
- String to search for in the database.
- {new}
- String to replace the found string with.
- [{table}...]
- List of database tables to search. Wildcards are supported, such as 'wp_*options' or 'wp_post*'.
- [--dry-run]
- Perform the entire search/replace operation and display the report, but do not actually make changes in the database.
- [--network]
- Search/replace across all tables registered in $wpdb in a multisite installation.
- [--all-tables-with-prefix]
- Include replacements for any tables matching the table prefix, even if they are not registered in $wpdb.
- [--all-tables]
- Include replacements for all tables in the database, regardless of the prefix or their registration in $wpdb. Overrides
--network
and--all-tables-with-prefix
options. - [--skip-tables={tables}]
- Skip replacements in specified tables. Use commas to specify multiple tables.
- [--include-columns={columns}]
- Perform replacements in specified columns. Use commas to specify multiple columns.
- [--skip-columns={columns}]
- Do not perform replacements in specified table columns. Use commas to specify multiple columns.
- [--export[={file}]]
- Write the transformed data to a SQL file without saving changes to the database. If {file} is not specified, output is sent to standard output (STDOUT).
- [--export_insert_size={rows}]
- Specifies the number of rows in a single INSERT statement during SQL export. This parameter may need to be adjusted depending on the database configuration (e.g., if fewer queries are required).
Default: 50 - [--precise]
- Force the use of the PHP query variant (instead of SQL). PHP is more accurate but slower.
- [--recurse-objects]
- Enable recursion into objects for string replacement.
Default: true - [--no-recurse-objects]
- Disable recursion into objects for string replacement.
Default: false - [--verbose]
- Output strings to the console as they are updated.
- [--regex]
- Perform search using a regular expression (without delimiters).
Note: --regex works 15-20 times slower. - [--regex-flags={regex-flags}]
- PCRE modifiers for --regex (e.g., 'i' for case-insensitivity).
- [--regex-delimiter={regex-delimiter}]
- Delimiter used for the regular expression. It must be escaped if it appears in the regex string.
Default: result of chr(1) - [--regex-limit={regex-limit}]
- Maximum possible replacements for the regular expression. Affects each string value of an array when deserializing.
Default: -1 (unlimited) - [--format={format}]
- Output format of the result. Can be:
table
,count
.
Default: table - [--report]
- Generate a report.
Default: true - [--report-changed-only]
- Report only the changed fields.
Default: false (if logging is enabled then true) - [--log[={file}]]
- File for logging. If {file} is not specified or is set to
-
, output is sent to STDOUT.
Note: slows down the operation, similar to or worse than enabling --precise or --regex. - [--before_context={num}]
- Number of characters to display before the old match and the new replacement. Ignored if logging is disabled.
Default: 40 - [--after_context={num}]
- Number of characters to display after the old match and the new replacement. Ignored if logging is disabled.
Default: 40
Usage
wp search-replace {old} {new} [{table}...] [--dry-run] [--network] [--all-tables-with-prefix] [--all-tables] [--skip-tables={tables}] [--include-columns={columns}] [--skip-columns={columns}] [--export[={file}]] [--export_insert_size={rows}] [--precise] [--recurse-objects] [--no-recurse-objects] [--verbose] [--regex] [--regex-flags={regex-flags}] [--regex-delimiter={regex-delimiter}] [--regex-limit={regex-limit}] [--format={format}] [--report] [--report-changed-only] [--log[={file}]] [--before_context={num}] [--after_context={num}]
Examples
IMPORTANT! On some servers (particularly on Windows), you need to use double quotes ("
instead of '
) to format search phrases. Phrases can also be used without enclosing them in quotes, so if the search phrases do not contain spaces, do not use quotes!
# 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) #!/bin/bash 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 --skip-tables=wp_users else wp search-replace 'http://example.com' 'http://example.dev' --recurse-objects --skip-columns=guid --skip-tables=wp_users fi
WP CLI search and replace URL for WordPress multisite
wp search-replace example.com newexample.com --precise --network