如何在DB中删除重复的Wordpress帖子

时间:2017-04-21 00:25:34

标签: sql wordpress mariadb

当我在我的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"

由于

3 个答案:

答案 0 :(得分:1)

以下是我设法解决问题的方法......

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

首先,我运行此查询以查看有多少帖子标题有撇号:

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

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

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

它返回了720个结果。所以我想我会替换所有&amp;#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'

答案 1 :(得分:0)

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'

答案 2 :(得分:0)

以上方法可能很慢。您可以用大男孩的方式来做...

-创建临时表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;