While managing WordPress content, finding and replacing content or titles in all articles can become necessary when you want to make changes to your entire site simultaneously. Whether you want to change a specific word, a piece of text, a link or another element, ItsmeIT provides you with 2 ways to do this easily and efficiently.
How to search and replace text in WordPress with Plugin & SQL
Note before continuing: You need to back up your database before proceeding. If you make a mistake during the implementation process, it could potentially damage your website.
Method 1: Use the Better Search Replace Plugin to replace the entire content
Click on the link provided at the end of the article to download the Better Search Replace Pro v1.3.2 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 1.3.2 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.
- 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.
As you can see, the original occurrence of “Plugin and SQL” in both the title and content has been replaced with “Plugin & SQL”. However, there is one issue: if you only want to change the content and keep the title unchanged, this plugin may not be suitable.
Based on my observation, it appears that the Better Search Replace plugin is designed to replace strings both in the content and the title. Therefore, if you use the plugin to replace links/URLs or phrases within the article that are not present in the title, it would be more appropriate.
Method 2: Replace the entire title or article with different content
With the second way to replace separate content for the entire title or post, which involves manual steps using phpMyAdmin, you can effectively search and replace text in WordPress. Unlike Method 1 that utilizes a plugin, this method allows you to specifically add, remove, or replace a phrase for the entire title or apply it to the article only, without altering everything site-wide.
Step 1: Login to phpMyadmin and search for the term to replace
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 “prefix table” applicable to your setup.
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. Continue reading and following ItsmeIT for further instructions.
Step 2: Replace the found string/phrase
Now, in your phpMyAdmin, you will see a list of all the posts containing the queried phrase. In my example from step 1, I will replace the phrase “Better Search Replace Pro v1.3.2” with “Better Search Replace Pro 1.3.2” (removing the ‘v’ before 1.3.2).
Please review and correct the following statement before executing the command on the SQL tab. Make sure to replace the appropriate phrase and “wp_” with the correct prefix table for your setup.
UPDATE wp_posts SET post_content = REPLACE(post_content, 'Better Search Replace Pro v1.3.2', 'Better Search Replace Pro 1.3.2') WHERE post_type = 'post' AND post_content LIKE '%Better Search Replace Pro v1.3.2%';
The above SQL snippet is used to execute an UPDATE query in the WordPress database. It has the function of replacing a specific text in the “post_content” column of the “wp_posts” table.
Explain each part of the SQL snippet:
UPDATE wp_posts: This is the beginning of the UPDATE query, which indicates that the table to be updated is “wp_posts”.
SET post_content = REPLACE(post_content, 'Better Search Replace Pro v1.3.2', 'Better Search Replace Pro 1.3.2'): This is the part that specifies the values to be changed. Specifically, it will replace the string “Better Search Replace Pro v1.3.2” with the string “ Better Search Replace Pro 1.3.2 ” in the column “post_content”. If you want to delete the string ” Better Search Replace Pro 1.3.2 “, then leave the replacement string empty.
WHERE post_type = 'post' AND post_content LIKE '%Better Search Replace Pro v1.3.2%': This is the conditional part of the UPDATE query. It specifies that only posts where the value of the “post_type” column is “post” and contains the string “Better Search Replace Pro v1.3.2” in the “post_content” column will be replaced. This helps ensure that only articles that meet this condition are updated.
The results after running the SQL command will provide you with the number of rows updated in the table and the query time. You can now open the post and verify the string you have replaced.
This is how you can search and replace text in WordPress. However, if you want to replace or delete a phrase in the entire title, here are the commands and instructions to accomplish the same process as changing the content.
The statement that queries the phrase/string in the title:
SELECT * FROM wp_posts WHERE post_type = 'post' AND post_title LIKE '%string to replace%';
Statement to replace phrase/string in title:
UPDATE wp_posts SET post_title = REPLACE (post_title, 'string to replace' , 'replace string' ) WHERE post_type = 'post' AND post_title LIKE '%string to replace%' ;