Managing WordPress sites often involves making changes across the database, such as updating URLs or replacing text strings. Thankfully, wp-cli, the command-line interface for WordPress, provides a powerful tool called search-replace to streamline this process.

The Command

wp search-replace 'old-url' 'new-url'

This command replaces all occurrences of 'old-url' with 'new-url' in the WordPress database.

Example Usage
Let's say we've migrated our WordPress site from http://example.com to https://example.com, and we need to update all instances of the old URL in the database. Here's how we would use wp-cli to accomplish this:

wp search-replace 'http://example.com' 'https://example.com'

Advanced Options
Dry Run:
To perform a dry run and see what changes would be made without actually modifying the database, add the --dry-run flag:

wp search-replace 'http://example.com' 'https://example.com' --dry-run

Specific Tables
You can limit the search and replace operation to specific database tables by specifying them:

wp search-replace 'http://example.com' 'https://example.com' --tables=wp_posts,wp_options

Conclusion
With wp-cli search-replace, updating URLs and text strings in your WordPress database becomes a breeze. Whether you're migrating your site, changing domain names, or making global updates, wp-cli empowers you to efficiently manage your WordPress content from the command line.