通过 SQL 查询替换帖子中的所有特定标签

问题描述 投票:0回答:1

由于需要花很长时间解释的原因,我在 WordPress 上发布的文章中没有任何标题 H2 标签,但 h3、h4、h5 和 h6 有。所以我必须修改所有其他标签,如下:H3 变成 H2,H4 变成 H3,H5 变成 H4...我的所有帖子都以 HTML 形式发布(不使用块或 Elementor),我准备通过手动完成Notepad++ 及其多重替换功能,因为我太害怕在操作数据库时犯错误。

现在我意识到我有 2x120 篇文章(该网站有 2 种语言)😬。所以我做了一个备份,并准备编写一个正则表达式来进行这些更改。但由于我的一些文章中也有“h2、h3、h4...”作为文本编写,因此我必须定位整个标签“

<h2>
</h2>
等”。像这样分开:

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

然后

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

其他标题标签依此类推。所以我想知道这是否安全,或者是否有更简单或更有效的方法。很抱歉提出“菜鸟”问题(对于某些人来说),但我承认我是一个会使用 SQL 查询的人。谢谢。

sql replace tags
1个回答
0
投票

首先,在进行任何更改之前,请确保您拥有 WordPress 数据库的完整备份。你不想把事情搞砸。

您可以在如下查询中尝试使用 case 语句(请参阅:SQL 中的 Case 语句),

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;

为了安全起见,尝试在开发数据库上运行查询(制作主数据库的副本),测试这是否有效,然后瞧。

© www.soinside.com 2019 - 2024. All rights reserved.