Searching through entire databases can be a hassle, but WP-CLI makes it easy. Here's how to search your WordPress site's database for the information you need.
Searching All Database Tables
Standard Behavior: Search All Registered Tables
By default, the standard database search command will only search for tables that are registered with $wpdb. In most cases, this is perfectly fine since it will include any standard WordPress core tables, as well as tables used by plugins and themes (as long as they're being properly registered).
wp db search 'my search string'
Optional: Search All Database Tables
Optionally, you can include all tables within the database, regardless of if they're properly prefixed or registered using $wpdb:
wp db search 'my search string' --all-tables
Searching Through Specific Tables
Do you already know what table to search in? You can specify them when running the command by passing them after your search string like this:
wp db search 'my search string' wp_posts
Or to specify multiple tables:
wp db search 'my search string' wp_posts wp_comments
Searching with Regex (Regular Expressions)
When searching for values that match a particular pattern, WP-CLI can be an incredibly handy too. Simply use regex within your database search query (without any flags) like this:
wp db search '^wp.*' --regex
If you want to pass additional regex flags within your database search, you can do so by using the --regex-flags parameter. For example, if you wanted to perform the same search as case-insensitive, it would look like this:
wp db search '^wp.*' --regex --regex-flags=i
We've only scratched the surface of the database search functionality within WP-CLI. For further information about additional parameters that can be passed for even further database searching power, see the official WP-CLI documentation on the wp db search command.