繁体   English   中英

如何删除数据库中重复的 Wordpress 帖子

[英]How delete duplicate Wordpress posts in DB

当我在 wp_posts 表(使用 phpmyadmin)上运行此代码时,它返回一个重复的帖子。

SELECT a.ID, a.post_title, a.post_type, a.post_status
FROM wp_posts AS a
 INNER JOIN (
   SELECT post_title, MIN( id ) AS min_id
   FROM wp_posts
   WHERE post_type = 'post'
   AND post_status = 'publish'
   GROUP BY post_title
   HAVING COUNT( * ) > 1
 ) AS b ON b.post_title = a.post_title
AND b.min_id <> a.id
AND a.post_type = 'post'
AND a.post_status = 'publish'

请注意,这个给定的帖子实际上不是重复的,它只有与另一个相同的标题(鉴于上面的代码查找重复标题的正常行为)。 然而,问题是数据库中实际上有数百个重复的帖子(如果我手动查看,我可以看到它们),那么为什么我不能使用上面的代码找到它们呢?

  • 我还可以从 wordpress 管理面板看到那两个重复的帖子......
  • 我还尝试使用一些 Wordpress 插件,它们都找到了与上述代码相同的重复帖子,但没有其他任何一个)。

编辑:如果我查看两个随机重复项,一个有:

ID 9462
post date 2017-03-07 13:06:31
post content "foo"
post title "Les pendules à l'heure"
post status "publish"
post type "post"
guid "http://www.exemple.com/?p=9462"

另一个有:

ID 11409
post date 2017-03-07 13:06:31
post content "foo"
post title "Les pendules à l&#039;heure"
post status "publish"
post type "post"
guid "http://www.exemple.com/?p=9462"

谢谢

这是我设法解决我的问题的方法...

我注意到即使重复帖子的标题看起来相同,一个有撇号,而另一个有 &#039 ;

所以首先我运行这个查询来查看有多少帖子标题有撇号:

SELECT post_title FROM wp_posts WHERE post_title LIKE "%'%",

它返回了 1825 个结果。 然后我运行以下命令来查看有多少帖子标题有 &#039 ;

SELECT post_title FROM wp_posts WHERE post_title LIKE "%&#039;%"

它返回了 720 个结果。 所以我想我会替换所有的 &#039 ; 通过撇号,使用以下查询:

UPDATE wp_posts SET post_title = REPLACE (post_title, '&#039;', '\'');

然后,我能够使用:

SELECT a.ID, a.post_title, a.post_type, a.post_status
FROM wp_posts AS a
 INNER JOIN (
   SELECT post_title, MIN( id ) AS min_id
   FROM wp_posts
   WHERE post_type = 'post'
   AND post_status = 'publish'
   GROUP BY post_title
   HAVING COUNT( * ) > 1
 ) AS b ON b.post_title = a.post_title
AND b.min_id <> a.id
AND a.post_type = 'post'
AND a.post_status = 'publish'

其中返回了 572 个帖子,我只是使用以下方法删除了这些帖子:

DELETE a.*
FROM wp_posts AS a
   INNER JOIN (
      SELECT post_title, MIN( id ) AS min_id
      FROM wp_posts
      WHERE post_type = 'post'
      AND post_status = 'publish'
      GROUP BY post_title
      HAVING COUNT( * ) > 1
   ) AS b ON b.post_title = a.post_title
AND b.min_id <> a.id
AND a.post_type = 'post'
AND a.post_status = 'publish'

上述方式可能会很慢。 你可以用BIG BOY的方式来做...

-- 创建临时表 CREATE TABLE posts_temp_table LIKE wp_posts;

-- 添加约束 ALTER TABLE posts_temp_table ADD UNIQUE(YOUR_UNIQUE_FIELD, id);

-- 复制数据 INSERT IGNORE INTO posts_temp_table SELECT * FROM wp_posts;

-- 重命名并删除重命名表 wp_posts 到 old_wp_posts,posts_temp_table 到 wp_posts; 删除表 old_wp_posts;

Try this

DELETE a.*
FROM wp_posts AS a
   INNER JOIN (
      SELECT post_title, MIN( id ) AS min_id
      FROM wp_posts
      WHERE post_type = 'post'
      AND post_status = 'publish'
      GROUP BY post_title
      HAVING COUNT( * ) > 1
   ) AS b ON b.post_title = a.post_title
AND b.min_id <> a.id
AND a.post_type = 'post'
AND a.post_status = 'publish'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM