SQL query delete duplicate posts

To delete posts with the same post title via SQL I run the following query on the database:

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';

I'm Robbert Vermeulen. I document everything I learn and help thousands of people with coding WordPress. My site has no ads or sponsors. If you enjoy my content, please consider supporting what I do.

Comments

    No comments yet..

Leave a reply

Feel free to ask questions or make comments.