Fix comment and category counts after import
-
I am on this forum all the time finding solutions to things and never put up a post. I have been fighting with the wordpress import tool always dropping the comment and category counts when I import posts from another blog. It is an annoying problem and I didn’t find a solution anywhere so I wrote a short script to count things up and update my taxonomy tables. It worked for me, maybe it will help someone else avoid wasting a bunch of time trying to figure out how to get their counts back. Create a php file, put in the code below, between the body tags, with your alterations and load it up to your site and navigate to it to run the script and update your counts. I grabbed the database info from the wp-config.php file, you can copy and paste those four lines in from that file. Change yourprefix in the code to the table prefix you chose, it is in the same file near the bottom (on my version of WP). I echoed the values so you can see the ids and counts as it loops through the tables.
<?php define('DB_NAME', 'your_db'); // The name of the database define('DB_USER', 'your_user'); // Your MySQL username define('DB_PASSWORD', 'your_password'); // ...and password define('DB_HOST', 'localhost'); // 99% chance you won't need to change this value if (!mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)) { die('Could not connect: ' . mysql_error()); } if (!mysql_select_db(DB_NAME)) { die('Could not connect: ' . mysql_error()); } $result = mysql_query("SELECT term_taxonomy_id FROM yourprefix_term_taxonomy"); while ($row = mysql_fetch_array($result)) { $term_taxonomy_id = $row['term_taxonomy_id']; echo "term_taxonomy_id: ".$term_taxonomy_id." count = "; $countresult = mysql_query("SELECT count(*) FROM yourprefix_term_relationships WHERE term_taxonomy_id = '$term_taxonomy_id'"); $countarray = mysql_fetch_array($countresult); $count = $countarray[0]; echo $count."<br />"; mysql_query("UPDATE yourprefix_term_taxonomy SET count = '$count' WHERE term_taxonomy_id = '$term_taxonomy_id'"); } $result = mysql_query("SELECT ID FROM yourprefix_posts"); while ($row = mysql_fetch_array($result)) { $post_id = $row['ID']; echo "post_id: ".$post_id." count = "; $countresult = mysql_query("SELECT count(*) FROM yourprefix_comments WHERE comment_post_ID = '$post_id' AND comment_approved = 1"); $countarray = mysql_fetch_array($countresult); $count = $countarray[0]; echo $count."<br />"; mysql_query("UPDATE yourprefix_posts SET comment_count = '$count' WHERE ID = '$post_id'"); } ?>
- The topic ‘Fix comment and category counts after import’ is closed to new replies.