Skip to main content
All CollectionsSupplementary GuidesWordPressGeneral
How to Perform a WordPress Search and Replace
How to Perform a WordPress Search and Replace

Learn how to update URLs, text, and database values in WordPress safely using plugins, WP-CLI, or phpMyAdmin.

Rapyd Team avatar
Written by Rapyd Team
Updated over a month ago

Search and replace is a powerful tool for making mass updates to your WordPress database. This is especially helpful when updating URLs, such as changing from http to https, or when replacing specific words, phrases, or characters. This guide walks you through what search and replace is, why and when you might need it, and how to perform it safely using a plugin, WP-CLI, or phpMyAdmin.


What Is Search and Replace?

Search and replace involves locating specific text, URLs, or values in your WordPress database and replacing them with updated information. This process ensures consistent updates without manually editing individual entries, saving time and effort.


Why Do a Search and Replace?

Search and replace can help in many scenarios, such as:

  • Updating URLs from http to https after enabling an SSL certificate.

  • Switching URLs between www and non-www formats.

  • Replacing environment domain URLs with a primary domain after setting it.

  • Making mass updates to specific words, phrases, or values across your database.

Important: Always back up your database before performing a search and replace to prevent data loss or corruption.


How to Perform a Search and Replace

Using a Plugin

The easiest way to perform a search and replace is with a WordPress plugin like Better Search Replace. This method is beginner-friendly and requires no coding knowledge.

Steps:

  1. Install the Plugin:

    • Go to your WordPress admin dashboard.

    • Navigate to Plugins > Add New, search for Better Search Replace, and click Install Now followed by Activate.

  2. Run the Search and Replace:

    • Navigate to Tools > Better Search Replace.

    • In the Search for field, enter the old value (e.g., http://example.com).

    • In the Replace with field, enter the new value (e.g., https://example.com).

    • Select the tables to update. Usually, selecting all tables is appropriate.

  3. Perform a Dry Run:

    • Check the Run as dry run? option to preview changes without making updates.

  4. Execute the Search and Replace:

    • Uncheck the dry run option and click Run Search/Replace.

    • Verify the changes have been applied successfully.

Use Case Example: Update all database entries from http://example.com to https://example.com.


Using WP-CLI

WP-CLI is a powerful tool for advanced users comfortable with the command line. It allows you to perform search and replace operations across your WordPress database efficiently.

Disclaimer: WP-CLI commands directly modify your database. Use caution and back up your database before proceeding.

Steps:

  1. Access WP-CLI:

  2. Run the Command:
    Use the following WP-CLI command to replace http with https:

    wp search-replace 'http://example.com' 'https://example.com' --all-tables --precise --recurse-objects --skip-columns=guid

    Explanation of flags:

    --all-tables: Searches all database tables.

    --precise: Ensures accurate replacements.

    --recurse-objects: Updates serialized data, preserving integrity.

    --skip-columns=guid: Avoids updating guid fields, which should remain unchanged for RSS feeds and post links.

  3. Preview Changes:
    Add the --dry-run flag to simulate the changes without applying them:

    wp search-replace 'http://example.com' 'https://example.com' --all-tables --precise --recurse-objects --skip-columns=guid --dry-run

  4. Apply Changes:
    Once satisfied, run the command without the --dry-run flag to apply the changes.

Use Case Example: Transition your site from http to https after enabling SSL.

Link to WP-CLI Documentation: WP-CLI Search and Replace


Using phpMyAdmin

For those who prefer working directly with the database, phpMyAdmin allows you to execute a MySQL query for search and replace.

However, the SQL query provided below is not completely safe for all cases in WordPress because it does not handle serialized data properly, except for very specific fields like siteurl and home in the wp_options table. Direct SQL queries should be used only when you're absolutely sure that you're not modifying serialized data.

Direct SQL Query Caveats

  • Risk of Breaking Serialized Data: WordPress uses serialized data for storing arrays and objects, especially in fields like meta_value and option_value. A simple SQL REPLACE statement will not adjust the length of serialized strings, leading to data corruption.

  • Limited Safe Usage: The provided SQL query is safe only for specific fields that do not store serialized data, such as siteurl, home, guid, or plain text fields in post_content or comment_content.

We do not recommend this option unless you are an experienced WordPress developer. Even if that is the case, we strongly recommend WP-CLI. Make sure to backup your database before continuing!

Steps:

  1. Access phpMyAdmin:

    Log into your hosting dashboard and navigate to Settings > Database > phpMyAdmin.

  2. Backup Your Database:

    Before making changes, export your database as a backup from phpMyAdmin. Learn how to do this in this guide: How to Import and Export Your Database with phpMyAdmin

  3. Locate Your Database:

    Select your WordPress database from the list on the left.

  4. Run SQL Queries:

    If you absolutely must run a raw SQL query in phpMyAdmin, here's a modified approach to avoid altering serialized data. Use queries only for non-serialized fields, such as updating URLs in the wp_posts and wp_comments tables:

    1. UPDATE wp_options SET option_value = REPLACE(option_value, 'http://example.com', 'https://example.com') WHERE option_name IN ('siteurl', 'home');

    2. UPDATE wp_posts SET guid = REPLACE(guid, 'http://example.com', 'https://example.com');

    3. UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://example.com', 'https://example.com');

    4. UPDATE wp_comments SET comment_content = REPLACE(comment_content, 'http://example.com', 'https://example.com');

    Important Notes:

    • Replace http://example.com with your old URL and https://example.com with the new URL.

    • Adjust the table prefixes (wp_) if your database uses a custom prefix.

    • This query is not safe for fields that store serialized data (e.g., meta_value in wp_postmeta or wp_usermeta).

    • Always back up your database before running the query.

  5. Test Your Site:

    Verify that all links, images, and resources have been updated correctly.

For a safe search and replace, prefer WP-CLI or the Better Search Replace plugin. Direct SQL queries are fine for very specific use cases (e.g., updating siteurl and home values), but they should never be used on serialized fields.


Conclusion

Performing a search and replace in WordPress is an essential task when migrating a site, changing a domain, or making bulk content updates. By using a plugin, WP-CLI, or phpMyAdmin, you can quickly and safely update your database. Always remember to back up your database before making changes, and test your site thoroughly afterward.

For further assistance, contact our support team or explore additional resources in our knowledge base.


Let me know if you'd like further adjustments! 😊

Did this answer your question?