Similar to Microsoft Word and Google Docs’ Find and Replace function, the WordPress search and replace option allows you to search for a string through all the rows in a table, then replace that string with another one at once.
It is on the list of the most powerful features of WordPress CLI. You can use it to replace some text, files, or even URLs on multiple pages or posts with an alternative.
However, there are still times that your WP search-replace option doesn’t work due to a bug in the WP CLI. That’s why in this article, we’ll explain what WP CLI search-replace is and how to utilize it correctly. We’ll also wrap up this post by introducing the guide to updating your site URL with WP-CLI.
When Using WordPress Search and Replace
There are various situations in which you need to do a search and replace action on your WordPress website.
- Misspelling and typos
You have a few grammatical mistakes in many of your blogs without notice. Instead of going back to individual articles, reviewing them one by one, and updating the errors, you should have these text corrected automatically.
- Deleting characters
Some WordPress updates may end up adding weird characters and symbols to your database. It’s necessary to remove them to keep your database clean. Another case comes with changing your brand name. If you add the site name to SEO titles automatically, you’re required to update all your old titles with the new site name.
- Replace protected image URLs
You decide to make money from photos and images displayed on your site by protecting them from the public and allowing members and paid customers to view them.
Rather than going to many posts and taking them down manually, let the search and replace function handle the hard work. The same thing goes for updating HTTP URLs to HTTPS after a migration or moving your website from non-www to www.
WP CLI Search-replace and How to Use It
WP CLI search-replace gives you a helping hand in replacing strings in specific database tables. It best serves post content updates and changing URLs for sites on a WordPress multisite.
WP CLI search-replace’s structure looks something like this:
wp search-replace 'source' 'target'
The ‘source’ option presents what you’re searching for while ‘target’ is the word or phrase you’re going to use to replace the source. This structure applies to all database tables. In case you plan to search in a certain table, here is how it looks:
wp search-replace 'source' 'target' wp_posts wp_options
It’s possible for you to add flags to WP CLI Search-replace to ensure more right search-replace results.
We highly recommend running the command first with this –dry-run option. It’ll give you the pretended results of the command before having to actually start the search-replace performance. Hence, you can verify the results in advance instead of making changes in the database directly.
You should add the –dry-run option to the bottom of the command so you can easily delete it when running the final command.
Including the –report-changed-only option in a search-replace command means that it will report just the fields and tables affected by the command. It runs properly with the –dry-run option. This helps shorten and clean up your report for better readability, especially with large databases or multisite.
You’ll run this –network option on a multisite to turn on replacement on all subsites in the website database. Unless you use this flag, it applies to selected subsite’s tables only.
–skip-plugins and –skip-themes
This flag blocks plugin or theme code from rendering when running WP CLI commands. You can use this when running commands and the site returns an error.
Use WP-CLI Like a Pro
WordPress search-replace function makes it easy for you to find and fix typos or grammar errors, update URLs, and remove characters. Due to a bug in WP-CLI, your WP search-replace doesn’t work as expected.
You need to add multiple flags to the WP CLI search-replace and separate them by a space to achieve the desired search and replace result.
You can learn 3 ways to perform database search and replace in WordPress. Pick one of these 5 WordPress Search Replace Plugins for great WordPress search-replace assistance.