Advertisement
bendy303

WP: Fix count of custom taxonomies and comments in admin after import, updated from PHP5 for PHP7

Feb 16th, 2021
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. <?php
  2. //updated code from PHP5 to PHP7.
  3. // from https://www.wpbeginner.com/wp-tutorials/how-to-fix-category-and-comment-count-after-wordpress-import/
  4.  
  5.  
  6. include("wp-config.php");
  7. $myConnection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
  8. if (!mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD)) {  die('Could not connect: ' . mysqli_error());  }
  9. if (!mysqli_select_db($myConnection, DB_NAME)) {  die('Could not connect: ' . mysqli_error());  }
  10.  
  11. $result = mysqli_query($myConnection, "SELECT term_taxonomy_id FROM ".$table_prefix."term_taxonomy");
  12. while ($row = mysqli_fetch_array($result)) {
  13.   $term_taxonomy_id = $row['term_taxonomy_id'];
  14.   echo "term_taxonomy_id: ".$term_taxonomy_id." count = ";
  15.   $countresult = mysqli_query($myConnection, "SELECT count(*) FROM ".$table_prefix."term_relationships WHERE term_taxonomy_id = '$term_taxonomy_id'");
  16.   $countarray = mysqli_fetch_array($countresult);
  17.   $count = $countarray[0];
  18.   echo $count."<br />";
  19.  mysqli_query($myConnection, "UPDATE ".$table_prefix."term_taxonomy SET count = '$count' WHERE term_taxonomy_id = '$term_taxonomy_id'");
  20.         }
  21.  
  22. $result = mysqli_query($myConnection, "SELECT ID FROM ".$table_prefix."posts");
  23. while ($row = mysqli_fetch_array($result)) {
  24.   $post_id = $row['ID'];
  25.   echo "post_id: ".$post_id." count = ";
  26.   $countresult = mysqli_query($myConnection, "SELECT count(*) FROM ".$table_prefix."comments WHERE comment_post_ID = '$post_id' AND comment_approved = 1");
  27.   $countarray = mysqli_fetch_array($countresult);
  28.   $count = $countarray[0];
  29.   echo $count."<br />";
  30.   mysqli_query($myConnection, "UPDATE ".$table_prefix."posts SET comment_count = '$count' WHERE ID = '$post_id'");
  31.         }
  32. ?>
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement