How to Search and Replace in Bulk Using WP CLI?

Nashwan Doaqan
1 min readJul 30, 2019

There are several occasions when you want to do a mass search and replace process for a broad set of strings in your WordPress site’s database, and you may need to replace an extensive collection of broken URLs or Keywords. WordPress has some plugins like “Better Search Replace” and “Search & Replace”. However, these solutions only provide the ability to search and replace one string at a time, and they might reach a bottleneck if you have a large database.

One idea is to create a CSV file that contains the strings with their replacements. We can then leverage WP CLI tool to loop through all the records one-by-one and do search-replace automatically by using $IFS and WHILE as the example below:

Important Note: Don’t forget to try a dry-run before you save any changes to your database.

--

--