How To Merge WordPress Comments From One Post to Another

How To Merge WordPress Comments From One Post to Another

Last updated on

There is no reliable plugin currently available that can merge comments from one WordPress post to another. Instead, you must run a database query to carry out this task.

First, you must get the target post ID, e.g. the post you want to move comments to. You can find this post ID in the address bar when you edit the post (see above).

Next, get the source post ID, e.g. the post you want to move comments from.

Log into MySQL or phpMyAdmin and run the following query.

Warning: Back up you database before running this query because it cannot be reversed. I repeat, back up your database!

UPDATE wp_comments SET comment_post_ID='target_ID' WHERE comment_post_ID ='source_ID';

Recount Comments?

The comment count for each post is stored in wp_posts under the column comment_cnt, because obviously you can’t expect WordPress to count all comments on every page load, that would be a horrible performance hit.

comment_cnt is updated when someone posts a new comment, however, you can force the database to recount comments for all posts with this PHP script below. Again, I warn you, you must backup your database before running this.

<?php
$entries = $wpdb->get_results("SELECT * FROM wp_posts WHERE post_type IN ('post', 'page')");

foreach($entries as $entry)
{
    $post_id = $entry->ID;
    $comment_count = $wpdb->get_var("SELECT COUNT(*) AS comment_cnt FROM wp_comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1'");
    $wpdb->query("UPDATE wp_posts SET comment_count = '$comment_count' WHERE ID = '$post_id'");
}

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

Leave a reply

Your email address will not be published. Required fields are marked *