WP-CLI: search-replace your WordPress DB from command line

Especially if you don't use a formal dev-to-prod procedure, using wp-cli to search-replace your WP DB can be quick and convenient. Use with caution!


WP-CLI is available on all Merlot Digital Servers, so you're able to jump straight in and make the most of WordPress!

Here's a quick run-through of how to perform a search-replace across the WP DB using the wp-cli tooling only. :-)

  1. Make sure secure shell (SSH) is enabled for the cPanel account which contains the WP install you're modifying.
    1. Login to WHM, go to "Manage Shell Access" and enable Secure Shell for the account/s you want to access.
    2. Make sure you roll passwords (and make them sufficiently secure), and ideally use SSH key-pairs instead.
  2. Connect to the account via SSH. On macOS this is done via Terminal, and on Windows this can be done in PuTTY.
    1. We use the default SSH port for most instances. It should drop you in ~ (user homedir) upon login success.
  3. To perform a search-replace across the WordPress Database, use the below after understanding each command.
    1. cd ~/public_html
    2. wp search-replace 'staging-website.com' 'production-website.com' --dry-run 
    3. wp search-replace 'staging-website.com' 'production-website.com'
    4. wp maintenance-mode status

Here you can see example output from a dry-run using the above syntax, so you know what to expect (roughly):

[testing@host11 public_html]$ wp search-replace 'EXAMPLE1.com' 'EXAMPLE2.com' --dry-run 
+------------------+-----------------------+--------------+------+
| Table            | Column                | Replacements | Type |
+------------------+-----------------------+--------------+------+
| wp_commentmeta   | meta_key              | 0            | SQL  |
| wp_commentmeta   | meta_value            | 0            | SQL  |
| wp_comments      | comment_author        | 0            | SQL  |
| wp_comments      | comment_author_email  | 0            | SQL  |
| wp_comments      | comment_author_url    | 0            | SQL  |
| wp_comments      | comment_author_IP     | 0            | SQL  |
| wp_comments      | comment_content       | 0            | SQL  |
| wp_comments      | comment_approved      | 0            | SQL  |
| wp_comments      | comment_agent         | 0            | SQL  |
| wp_comments      | comment_type          | 0            | SQL  |
| wp_links         | link_url              | 0            | SQL  |
| wp_links         | link_name             | 0            | SQL  |
| wp_links         | link_image            | 0            | SQL  |
| wp_links         | link_target           | 0            | SQL  |
| wp_links         | link_description      | 0            | SQL  |
| wp_links         | link_visible          | 0            | SQL  |
| wp_links         | link_rel              | 0            | SQL  |
| wp_links         | link_notes            | 0            | SQL  |
| wp_links         | link_rss              | 0            | SQL  |
| wp_options       | option_name           | 0            | SQL  |
| wp_options       | option_value          | 12           | PHP  |
| wp_options       | autoload              | 0            | SQL  |
| wp_postmeta      | meta_key              | 0            | SQL  |
| wp_postmeta      | meta_value            | 72           | PHP  |
| wp_posts         | post_content          | 249          | SQL  |
| wp_posts         | post_title            | 0            | SQL  |
| wp_posts         | post_excerpt          | 0            | SQL  |
| wp_posts         | post_status           | 0            | SQL  |
| wp_posts         | comment_status        | 0            | SQL  |
| wp_posts         | ping_status           | 0            | SQL  |
| wp_posts         | post_password         | 0            | SQL  |
| wp_posts         | post_name             | 0            | SQL  |
| wp_posts         | to_ping               | 0            | SQL  |
| wp_posts         | pinged                | 0            | SQL  |
| wp_posts         | post_content_filtered | 0            | SQL  |
| wp_posts         | guid                  | 247          | SQL  |
| wp_posts         | post_type             | 0            | SQL  |
| wp_posts         | post_mime_type        | 0            | SQL  |
| wp_term_taxonomy | taxonomy              | 0            | SQL  |
| wp_term_taxonomy | description           | 0            | SQL  |
| wp_termmeta      | meta_key              | 0            | SQL  |
| wp_termmeta      | meta_value            | 0            | SQL  |
| wp_terms         | name                  | 0            | SQL  |
| wp_terms         | slug                  | 0            | SQL  |
| wp_usermeta      | meta_key              | 0            | SQL  |
| wp_usermeta      | meta_value            | 1            | PHP  |
| wp_users         | user_login            | 0            | SQL  |
| wp_users         | user_nicename         | 0            | SQL  |
| wp_users         | user_email            | 0            | SQL  |
| wp_users         | user_url              | 2            | SQL  |
| wp_users         | user_activation_key   | 0            | SQL  |
| wp_users         | display_name          | 0            | SQL  |
+------------------+-----------------------+--------------+------+
Success: 583 replacements to be made.
[testing@host11 public_html]$ 

If you'd like more information about WP-CLI, see their website and the command reference below to keep learning.


Otherwise feel free to get in touch - it's always best to check before running commands, just in case!

  • 0 users found this helpful
Was this answer helpful?

Similar, and hopefully helpful

Website cloning: How to duplicate a WordPress website (cP)

If you're not using a system like WordPress Toolkit for staging, you can easily clone sites...

WP-CLI: Using the command line to update your WordPress install

If you're managing a range of sites, or even a single busy website, WP-CLI can save you valuable...

NGINX Caching: How to exclude a URL / slug from User Cache

If you have a custom login URL, eCommerce jobs/queries or unique requirements, you can exclude...