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 2x120 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.
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.