m.javad Koushki
m.javad Koushki

Reputation: 39

Delete duplicate posts in WordPress

I want to delete duplicate posts from my WordPress website select duplicate posts work currectlly

 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'

and

this query that i need has error
    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'

this is error message :

Unexpected token (near a) Unexpected token (near .) Unexpected token (near *)

Upvotes: 0

Views: 661

Answers (1)

Lahiru Jayakody
Lahiru Jayakody

Reputation: 5400

First SELECT post IDs. Then Use DELETE statement with WHERE IN clause.

NOTE: Before executing whole query, test whether sub query in IN() clause returns correct ids.

DELETE FROM wp_posts
WHERE ID IN (
    SELECT a.ID
    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'
)

Upvotes: 3

Related Questions