Replacing Content on Wordpress Pages with SQL

So recently we had the issue that we had duplicate <h1> tags on some of our client pages. Well having duplicate <h1> tags is not good for SEO purposes, so this was bad. But imagine having close to 50 pages, this would take a long time to go and manually go into each page and change this.

SQL was the perfect solution here, SQL is the language the databases talk, so we needed to go into our database and change this there. But anytime you start messing with the database there could be problems. You can delete your content in a blink of an eye. You gotta be careful. So we made a copy of our database.

We used phpMyAdmin and then went into Export and pressed Go, immediately your database is downloaded. This was pretty easy.

Where does your content live? In our case we wanted to modify pages. Both blog posts and pages are stored under the table wp_posts. It’s a very good idea to duplicate this table if you wanted too. Just one extra step in case something goes bad and you delete your table. We actually didn’t lol. We felt pretty comfortable going forward. If something would have happened we probably would have to upload the database copy we downloaded earlier.

Now comes the hard part. Under SQL still in phpMyAdmin go into SQL. And there you will find a box where you will input the SQL code to execute.

Let me explain what’s going on here, the 1st line says UPDATE wp_posts, here we’re basically telling it to update table wp_posts. The second line is the line that replaces the content, in our case we replaced <h1 for <h2. So basically here first goes the content to be replaced and secondly what you need to replace it with. The 3rd line limits what you’re replacing. This basically tells it to only replace content from WordPress pages and not blog posts or any other type of post type.

That’s it. Just make sure you really check the code several times and make sure you download the complete database.

UPDATE wp_posts
SET post_content = REPLACE ( post_content, 'content to be replaced', 'replace with this' )
WHERE post_type='page';

Here is the code if case you want to copy and paste but make sure your table is named the same, many times with WordPress automatic installations the table name starts differently.