Removing WordPress Duplicate Comments

How to Remove Duplicate WordPress Comments

Looking on internet to find a way to delete WordPress duplicate comments and I found a partial solution to delete WordPress duplicate comments. I found a PHP script which had to be modified to work.

In the next words I will show you the script which will delete permanently duplicate comments.

How to remove WordPress duplicates:

You have to use this PHP script:

require_once('wp-config.php'); global $wpdb, $comment_content, $comments, $comment_ID;

# First select all comments
$query = "SELECT `comment_ID`, `comment_post_ID`, `comment_content` FROM ".$wpdb->comments." WHERE 1";
$comments = $wpdb->get_results($query);

# Array to hold keeper comment IDs so we don't delete them if there are doops
$keeper_comments = array();

# Now check if each comment has any matching comments from the same post
foreach ($comments as $comment)
{
    $query = "SELECT `comment_ID` FROM ".$wpdb->comments." WHERE `comment_ID` != ".$comment->comment_ID." AND `comment_post_ID` = ".$comment->comment_post_ID." AND `comment_content` = '".addslashes($comment->comment_content)."'";
    $matching_comments = $wpdb->get_results($query);
    if ($wpdb->num_rows > 0)
    {
        foreach ($matching_comments as $matching_comment)
        {
            if (!in_array($matching_comment->comment_ID, $keeper_comments))
            {
                $wpdb->query("DELETE FROM ".$wpdb->comments." WHERE `comment_ID` = ".$matching_comment->comment_ID);
                $wpdb->query("UPDATE ".$wpdb->posts." SET `comment_count` = `comment_count` - 1 WHERE `comment_ID` = ".$matching_comment->comment_ID);
            }
        }
        $keeper_comments[] = $comment->comment_ID;
    }
}

Copy this code >> Save as delete_duplicate_comments.php >> upload to root folder of your website and then access it as http://website.com/delete_duplicate_comments.php and that’s all, all your duplicate comments will go away forever.

Do not forget to delete the delete_duplicate_comments.php file from root of your website after you have deleted duplicates.

***The script should be safe but you should backup your database too before.

Conclusion

You know now how to remove WordPress duplicate comments.

And credits: https://gist.github.com/grimmdude/5828710

The original script from github was not working, this is why I have reposted with some changes to make it work.

Related posts:

One Comment on "Removing WordPress Duplicate Comments"

  1. shady says:

    saved my life man thanks

Got something to say? Go for it!