How to Search and Replace in the WordPress database using WP CLI

September 16, 2024 / WordPress

The WP-CLI command line tool lets you efficiently search and replace content in your WordPress database. This is what is explained in this article.

Prerequisites:

  1. SSH access to your server.
  2. WP-CLI installed.

Follow the steps:

  1. Use a terminal to connect to your server:
    ssh username@your-server-ip
  2. Navigate to the WordPress installation directory:
    cd /path/to/wordpress
  1. Run Search and Replace Command:
    1. Use this command to search and replace text in your database:
      wp search-replace ‘old-string’ ‘new-string’
    2. Example (changing site URLs):
      wp search-replace ‘http://oldsite.com’ ‘http://newsite.com’
  2. Optional:
    1. Use –dry-run to preview changes:
      wp search-replace ‘old-string’ ‘new-string’ --dry-run
    2. Use –all-tables to include non-standard tables:
      wp search-replace ‘old-string’ ‘new-string’ --all-tables
  3. Verify:
    Check your site to ensure the changes are reflected correctly.

Note: Always back up your database before making changes.

This is how you can perform a search and replace in the WordPress database using WP CLI. If you need assistance, don’t hesitate to contact our support team.

Prefer using plugins? Discover How to Use a Plugin to Perform a Search & Replace in the WordPress Database

Spread the love