skip to Main Content

For a reason that would take too long to explain, I don’t have any heading H2 tags in my articles published on my WordPress, but h3, h4, h5, and h6 yes. So I have to modify all the other tags as follows: H3 becomes H2, H4 becomes H3, H5 becomes H4… All my posts are published in HTML (without using blocks or Elementor), and I was preparing to do it manually via Notepad++ and its multiple replace feature because I’m too afraid of making a mistake when manipulating the database.

Now I realize that I have 2×120 articles (the site is in 2 languages) 😬. So I made a backup and am about to write a regular expression to make these changes. But as there are also "h2, h3, h4…" written as text in some of my articles, I’m going to have to target the entire tags "<h2>, </h2>, etc." separately like this:

UPDATE wp_posts SET post_content = REPLACE (post_content, '<h3>', '<h2>');

then

UPDATE wp_posts SET post_content = REPLACE (post_content, '</h3>', '</h2>');

and so on for the other headings tags. So I was wondering if this is safe, or if there’s a simpler or more effective way. Sorry for the "noob" question (for some), but I admit I’m one, with SQL queries 😅. Thanks.

2

Answers


  1. Chosen as BEST ANSWER

    Just so you know, I finally opted for a "safer" approach: I downloaded the "wp_posts" table, edited it with Notepad++ and then made the changes using the "search and replace" tool. Operation successfully completed in two minutes (over 3000 headings replaced). I should have thought of this sooner. 🙄


  2. First of all, before making any changes, ensure you have a complete backup of your WordPress database. You don’t want to mess it up.

    And you could try the case statements (Refer: Case Statements in SQL) in a query like following,

    UPDATE wp_posts
    SET post_content = 
    CASE
       WHEN post_content LIKE '%<h3>%' THEN REPLACE(post_content, '<h3>', '<h2>')
       WHEN post_content LIKE '%</h3>%' THEN REPLACE(post_content, '</h3>', '</h2>')
       WHEN post_content LIKE '%<h4>%' THEN REPLACE(post_content, '<h4>', '<h3>')
       WHEN post_content LIKE '%</h4>%' THEN REPLACE(post_content, '</h4>', '</h3>')
       WHEN post_content LIKE '%<h5>%' THEN REPLACE(post_content, '<h5>', '<h4>')
       WHEN post_content LIKE '%</h5>%' THEN REPLACE(post_content, '</h5>', '</h4>')
       WHEN post_content LIKE '%<h6>%' THEN REPLACE(post_content, '<h6>', '<h5>')
       WHEN post_content LIKE '%</h6>%' THEN REPLACE(post_content, '</h6>', '</h5>')
       ELSE post_content
    END;
    

    And just to be safe try running queries on a dev db (make a copy of your main database), test if this works and then voila.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search