◦ Comprehensive security
◦ 24/7 support
WordPress Guide → WP-CLI → Database Search
WordPress database search: WP DB search with WP-CLI
Find your data quickly using the WordPress Command Line Interface (WP-CLI)! WordPress Database searching is at your fingertips, and here we inform you how!
Get fast, reliable hosting for WordPress
Power your site with the industry’s fastest, most optimized WordPress hosting
WordPress database search: How to search your database using WP-CLI
Trying to search your site’s database easily can be a pain, depending on which method you use. However, it is possible to use a tool with a user interface such as phpMyAdmin to try to query to find specific data records in the site’s database.
Reasons for needing to search your database
Reasons you would need to search your database would be finding a specific record in the database or finding where multiple records are being stored in the database. Trying to run the exact same search through wp-admin (WordPress dashboard) might be slow or time out depending on what is being searched and from which specific screen on the site.
Other methods to search your database
Other methods to search your database would be using an application like MySQL Workbench or Navicat. The Navicat application will allow you to select the object you wish to search, which would normally be the table. Use the Find button and double-click on the object to view the record in the structure. If you are using the MySQL Workbench application, then you can search table data and by selecting the specific table you want to search through.
Using a plugin such as Better Search Replace would be a much slower method since it would need to run in wp-admin than using the command provided in WP-CLI.
Why use WP-CLI
One of the most straightforward command-based methods to search your site’s database is to use WP-CLI. WP-CLI allows you to run anything that you would run from wp-admin (WordPress dashboard) but from the command line, which can be very handy if there is an issue on your site and you cannot access wp-admin.
How to use WP-CLI
To use WP-CLI on your site, you will need to find and then use the SSH credentials from the site’s portal. To use WP-CLI, you will need to use a command-line application that will depend on if you are using a Mac or PC. If using a Mac, you can use Terminal. If you are using a PC, then the recommendation would be to use Putty.
WP-CLI can only be used from the valid WordPress installation, which would be from:
cd public_html
If you are not in a proper WordPress installation, then WP-CLI will fail to use it. In addition, if a critical error is being caused on the site, then it will also cause WP-CLI not to work correctly.
If you need to search through all of the tables in the database using this WP-CLI command:
wp db search ‘foo’ –all-tables
If you need to be able to search through all of the tables in the site’s database that match the match the prefix that is set in the site’s wp-config.php file, then use this WP-CLI command:
wp db search ‘foo’ –all-tables-with-prefix
Searching all network tables
If you need to be able to search all of the database tables on a multisite network, then you can use this WP-CLI command:
wp db search ‘foo’ –network
Searching a specific table
If you want to target a specific database table, then you can set that in the WP-CLI command:
wp db search ‘foo’ wp_posts
Searching multiple tables
You can extend the WP-CLI command to cover multiple database tables (example targets the posts and comments database tables) using:
wp db search ‘foo’ wp_posts wp_comments
If you wanted to search even more database tables in this example, we are going to search posts, postmeta, comments, and commentmeta tables, and then you can include those in the WP-CLI command:
wp db search ‘foo’ wp_posts wp_postmeta wp_comments wp_commentmeta
Outputting statistics (“Stats”) when searching
You can output stats on the WP-CLI search command using the stats flag:
wp db search ‘my search string’ –stats
Output only matches
To be the able only output matches only, you can use the following WP-CLI:
wp db search ‘foo’ wp_posts –matches_only
Using regex to search
WP-CLI search command also supports using regex (regular expressions):
wp db search ‘^wp.*’ –regex
Using regex and case insensitivity
You can make the regex search case-insensitive, including a flag WP-CLI:
wp db search ‘^wp.*’ –regex –regex-flags=i
WP-CLI provides the power to run many complex searches or tasks but all from the command line and using commands that make sense and are easy to follow if you have used the command line before. Common tasks are always worth running through WP-CLI since they never have the delayed issues trying to run specific tasks in wp-admin (WordPress dashboard).
If you need to search through a multisite database at a specific subsite in a multisite, you could search through the domain string using this WP-CLI command:
wp db search example.com –url=example.com/foo
Power in using WP-CLI
The WP-CLI database search command is potent and makes it very easy to search strings and for specific references in your site’s database all from the command line. The command includes a number of flags and the global parameters that it supports.
Getting started with WP DB search with WP-CLI
When you’re ready to upgrade your WordPress hosting, Liquid Web can help. We’ve been offering the premium hosting for WordPress sites for decades—with the fastest speeds, best uptime guarantees, and most robust security features. Click below to explore options or start a chat to connect with one of our WordPress hosting experts right now.
Additional resources
What is WordPress? →
A complete beginner’s guide—from use cases, to basics, to how to get started
What is WP-CLI →
WP-CLI allows you to manage your users from the command line. This can come in handy when modifying privileges and changing roles quickly.
How to integrate WordPress and Slack →
If your org uses Slack and WordPress, there are several ways you can tie them together.
Luke Cavanagh, Strategic Support & Accelerant at Liquid Web, is one of the company’s most seasoned subject matter experts, focusing on web hosting, digital marketing, and ecommerce. He is dedicated to educating readers on the latest trends and advancements in technology and digital infrastructure.