Managing WordPress content sometimes requires finding and replacing specific text or titles across all your posts. Whether you need to update a word, phrase, link, or any other element, there are two efficient methods you can use: plugins and direct SQL queries.
Important Note: Before proceeding, always back up your WordPress database. Mistakes during the search and replace process could potentially harm your website.
Plugin or SQL? Choose Your Method for WordPress Search & Replace
Method 1: Replace All Content (Better Search Replace Plugin)
Download Better Search Replace Pro plugin, which is a powerful tool for search and replace text in WordPress. Install the plugin on your WordPress site and ensure successful activation. After installation, navigate to Tools -> Better Search Replace to access the configuration and settings of this plugin.
Here is the configuration information for the Better Search Replace Pro plugin:
- Search for: Enter the text you want to search and replace.
- Replace with: Text the content you need to replace the text of the search above;
- Select tables: Choose the table in the database that the plugin will scan and search. You can select one or more tables.
- To select multiple tables, hold down the Ctrl key and click on the tables you want to select.
- To select all tables, click on one table and press Ctrl + A.
- Case-Insensitive: This option determines whether the search is case-sensitive or not, allowing the plugin to differentiate between uppercase and lowercase letters.
- Replace GUIDs : If checked, the plugin will replace GUIDs in all tables with GUID headers.
- Run as dry run: Selecting this option will only show which tables contain the content you want to replace, without making any changes to your actual content.
Once you have finished configuring the settings, click on “Run Search/Replace” and the plugin will begin replacing the specified content in all posts on your WordPress site.
Note:
- The first time, it is recommended to select “Run as dry run” to allow the plugin to scan the tables containing the content you want to edit. It will display the number of results found so you can decide whether or not to proceed with the changes.
- The second time, after you have made the decision to proceed with the changes, uncheck “Run as dry run” and click the “Run Search/Replace” button to execute the actual content replacement.
Limitations: While this plugin is convenient for mass replacements, it might not be ideal if you only want to change text in specific areas (e.g., post content but not titles).
Method 2: Targeted Replacements with SQL Queries in phpMyAdmin
The second method involves manual steps using phpMyAdmin to search and replace text in WordPress. Unlike Method 1 (which uses a plugin), this approach gives you granular control, allowing you to target specific areas like the entire title or just the post content.
Step 1: Open phpMyAdmin and Run SQL Query
Most hosting providers or VPS services offer a database manager, typically phpMyAdmin. Log in to your phpMyAdmin account and navigate to the “SQL” tab as shown in the screenshot.
Proceed to copy and execute the following command. This command will search for the phrase “Better Search Replace Pro v1.3.2” within the entire content of the article. Copy the command, modify the phrase you wish to search for, and then click the “Go” button. Note that “wp_” should be replaced with the correct database table prefix for your WordPress installation.
SELECT * FROM wp_posts WHERE post_type = 'post' AND post_content LIKE '%Better Search Replace Pro v1.3.2%';
The query results will display the number of rows found, providing you with information on how many instances of the searched text were identified. This will help you make an informed decision regarding whether to proceed with the search and replace text in WordPress.
Step 2: Replace the Found String/Phrase
In phpMyAdmin, you’ll see a list of posts containing the phrase you searched for. To replace it:
- Review the Results: Carefully check the list to ensure you’re only editing the posts you intend to.
- Replace the Text: In the SQL tab, execute the following command (replace placeholders with your actual values):
UPDATE wp_posts SET post_content = REPLACE(post_content, 'old phrase', 'new phrase')
WHERE post_type = 'post' AND post_content LIKE '%old phrase%';
Note: To replace text in the title instead of the content, change post_content
to post_title
.
Remember, always exercise caution when working directly with your WordPress database. If you’re unsure about any step, consult the WordPress documentation or seek help from a developer. With the right tools and techniques, you can efficiently manage your content and keep your website up-to-date.