wp db search
Finds a string in the database.
$ wp db search <search> [<tables>...] [--network] [--all-tables-with-prefix] [--all-tables] [--before_context=<num>] [--after_context=<num>] [--regex] [--regex-flags=<regex-flags>] [--regex-delimiter=<regex-delimiter>] [--table_column_once] [--one_line] [--matches_only] [--stats] [--table_column_color=<color_code>] [--id_color=<color_code>] [--match_color=<color_code>]
Searches through all or a selection of database tables for a given string, Outputs colorized references to the string.
Defaults to searching through all tables registered to $wpdb. On multisite, this default is limited to the tables for the current site.
OPTIONS
- <search>
- String to search for. The search is case-insensitive by default.
- [<tables>...]
- One or more tables to search through for the string.
- [--network]
- Search through all the tables registered to $wpdb in a multisite install.
- [--all-tables-with-prefix]
- Search through all tables that match the registered table prefix, even if not registered on $wpdb. On one hand, sometimes plugins use tables without registering them to $wpdb. On another hand, this could return tables you don't expect. Overrides --network.
- [--all-tables]
- Search through ALL tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides --network and --all-tables-with-prefix.
- [--before_context=<num>]
- Number of characters to display before the match. --- default: 40 ---
- [--after_context=<num>]
- Number of characters to display after the match. --- default: 40 ---
- [--regex]
- Runs the search as a regular expression (without delimiters). The search becomes case-sensitive (i.e. no PCRE flags are added). Delimiters must be escaped if they occur in the expression.
- [--regex-flags=<regex-flags>]
- Pass PCRE modifiers to the regex search (e.g. 'i' for case-insensitivity).
- [--regex-delimiter=<regex-delimiter>]
- 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)
. - [--table_column_once]
- Output the 'table:column' line once before all matching row lines in the table column rather than before each matching row.
- [--one_line]
- Place the 'table:column' output on the same line as the row id and match ('table:column:id:match'). Overrides --table_column_once.
- [--matches_only]
- Only output the string matches (including context). No 'table:column's or row ids are outputted.
- [--stats]
- Output stats on the number of matches found, time taken, tables/columns/rows searched, tables skipped.
- [--table_column_color=<color_code>]
- Percent color code to use for the 'table:column' output. For a list of available percent color codes, see below. Default '%G' (bright green).
- [--id_color=<color_code>]
- Percent color code to use for the row id output. For a list of available percent color codes, see below. Default '%Y' (bright yellow).
- [--match_color=<color_code>]
- Percent color code to use for the match (unless both before and after context are 0, when no color code is used). For a list of available percent color codes, see below. Default '%3%k' (black on a mustard background).
The percent color codes available are:
Code | Color |
---|---|
%y | Yellow (dark) (mustard) |
%g | Green (dark) |
%b | Blue (dark) |
%r | Red (dark) |
%m | Magenta (dark) |
%c | Cyan (dark) |
%w | White (dark) (light gray) |
%k | Black |
%Y | Yellow (bright) |
%G | Green (bright) |
%B | Blue (bright) |
%R | Red (bright) |
%M | Magenta (bright) |
%C | Cyan (bright) |
%W | White |
%K | Black (bright) (dark gray) |
%3 | Yellow background (dark) (mustard) |
%2 | Green background (dark) |
%4 | Blue background (dark) |
%1 | Red background (dark) |
%5 | Magenta background (dark) |
%6 | Cyan background (dark) |
%7 | White background (dark) (light gray) |
%0 | Black background |
%8 | Reverse |
%U | Underline |
%F | Blink (unlikely to work) |
They can be concatenated. For instance, the default match color of black on a mustard (dark yellow) background %3%k
can be made black on a bright yellow background with %Y%0%8
.
EXAMPLES
# Search through the database for the 'wordpress-develop' string
$ wp db search wordpress-develop
wp_options:option_value
1:http://wordpress-develop.dev
wp_options:option_value
1:http://example.com/foo
...
# Search through a multisite database on the subsite 'foo' for the 'example.com' string
$ wp db search example.com --url=example.com/foo
wp_2_comments:comment_author_url
1:http://example.com/
wp_2_options:option_value
...
# Search through the database for the 'https?://' regular expression, printing stats.
$ wp db search 'https?://' --regex --stats
wp_comments:comment_author_url
1:https://wordpress.org/
...
Success: Found 99146 matches in 10.752s (10.559s searching). Searched 12 tables, 53 columns, 1358907 rows. 1 table skipped: wp_term_relationships.
# SQL search database table 'wp_options' where 'option_name' match 'foo'
wp db query 'SELECT * FROM wp_options WHERE option_name like "%foo%"' --skip-column-names
+----+--------------+--------------------------------+-----+
| 98 | foo_options | a:1:{s:12:"_multiwidget";i:1;} | yes |
| 99 | foo_settings | a:0:{} | yes |
+----+--------------+--------------------------------+-----+
# SQL search and delete records from database table 'wp_options' where 'option_name' match 'foo'
wp db query "DELETE from wp_options where option_id in ($(wp db query "SELECT GROUP_CONCAT(option_id SEPARATOR ',') from wp_options where option_name like '%foo%';" --silent --skip-column-names))"
GLOBAL PARAMETERS
--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 installation over HTTP.
--user=<id|login|email> Set the WordPress user.
--skip-plugins[=<plugins>] Skip loading all plugins, or a comma-separated list of plugins. Note: mu-plugins are still loaded.
--skip-themes[=<themes>] Skip loading all themes, or a comma-separated list of 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 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 arguments, or a subset specified as comma-separated values.
--quiet Suppress informational messages.
Checks the current status of the database.
Removes all tables with `$table_prefix` from the database.
Opens a MySQL console using credentials from wp-config.php
Displays information about a given table.
Creates a new database.
Deletes the existing database.
Exports the database to a file or to STDOUT.
Imports a database from a file or from STDIN.
Optimizes the database.
Displays the database table prefix.
Executes a SQL query against the database.
Repairs the database.
Removes all tables from the database.
Finds a string in the database.
Displays the database name and size.
Lists the database tables.