Skip to content

Instantly share code, notes, and snippets.

@iamjoshellis
Last active August 29, 2015 14:25
Show Gist options
  • Save iamjoshellis/85bdce54b73be4c214c3 to your computer and use it in GitHub Desktop.
Save iamjoshellis/85bdce54b73be4c214c3 to your computer and use it in GitHub Desktop.
Fix wordpress comment and category counts after import, by quarkin: https://wordpress.org/support/topic/fix-comment-and-category-counts-after-import
<?php
// Place this script in a page template and navigate to it. Script will correct counts for comments and categories.
include("wp-config.php");
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 ".$table_prefix."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 ".$table_prefix."term_relationships WHERE term_taxonomy_id = '$term_taxonomy_id'");
$countarray = mysql_fetch_array($countresult);
$count = $countarray[0];
echo $count."<br />";
mysql_query("UPDATE ".$table_prefix."term_taxonomy SET count = '$count' WHERE term_taxonomy_id = '$term_taxonomy_id'");
}
$result = mysql_query("SELECT ID FROM ".$table_prefix."posts");
while ($row = mysql_fetch_array($result)) {
$post_id = $row['ID'];
echo "post_id: ".$post_id." count = ";
$countresult = mysql_query("SELECT count(*) FROM ".$table_prefix."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 ".$table_prefix."posts SET comment_count = '$count' WHERE ID = '$post_id'");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment