Skip to content

Instantly share code, notes, and snippets.

@gus-costa
Created August 30, 2020 21:07
Show Gist options
  • Save gus-costa/752cfb233365c9458a23bc3bc2ae97e8 to your computer and use it in GitHub Desktop.
Save gus-costa/752cfb233365c9458a23bc3bc2ae97e8 to your computer and use it in GitHub Desktop.
Fix the categories and comments counts of your Wordpress blog. This is a PHP7 version of the script seen here: https://www.wpbeginner.com/wp-tutorials/how-to-fix-category-and-comment-count-after-wordpress-import/
<?php
include("wp-config.php");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
if ($result = $mysqli->query("SELECT term_taxonomy_id FROM " . $table_prefix . "term_taxonomy")) {
while ($row = $result->fetch_assoc()) {
$term_taxonomy_id = $row['term_taxonomy_id'];
echo "term_taxonomy_id: " . $term_taxonomy_id . " count = ";
if ($countresult = $mysqli->query("SELECT count(*) FROM " . $table_prefix . "term_relationships WHERE term_taxonomy_id = '$term_taxonomy_id'")) {
$countarray = $countresult->fetch_array();
$count = $countarray[0];
echo $count . "<br />";
$mysqli->query("UPDATE " . $table_prefix . "term_taxonomy SET count = '$count' WHERE term_taxonomy_id = '$term_taxonomy_id'");
$countresult->close();
}
}
$result->close();
}
if ($result = $mysqli->query("SELECT ID FROM " . $table_prefix . "posts")) {
while ($row = $result->fetch_assoc()) {
$post_id = $row['ID'];
echo "post_id: " . $post_id . " count = ";
if ($countresult = $mysqli->query("SELECT count(*) FROM " . $table_prefix . "comments WHERE comment_post_ID = '$post_id' AND comment_approved = 1")) {
$countarray = $countresult->fetch_array();
$count = $countarray[0];
echo $count . "<br />";
$mysqli->query("UPDATE " . $table_prefix . "posts SET comment_count = '$count' WHERE ID = '$post_id'");
$countresult->close();
}
}
$result->close();
}
$mysqli->close();
@tallesairan
Copy link

Awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment