Skip to main content

WordPress taxonomy terms and comments count showing zero after import

A client recently needed me to migrate a lot of posts and tags to WordPress from a different CMS.
After importing all of the tags and posts to WordPress, I noticed that If I went to the tags page, all of the tags were listed, but they were all showing as ‘0’ even though the tags were assigned to posts. This was specifically used for a custom post type and custom taxonomy.

To fix this issue, you will need to run a script that will allow your website to update the count to be up to date. Then, in future when you add tags to posts, it will all work fine, you just need to do it the first time as your import had not triggered the code in WordPress to update the count the first time.

What you need to do is, copy this code below and copy it into a new php file, called whatever you want. For example, fix.php.

Then, copy fix.php into your WordPress base folder. Not your theme folder, your main WordPress folder which contains your WP-CONFIG.php file.

Once you’ve copied it into that main folder, go to your web browser and run fix.php at your website address/fix.php

The script will then run, assigning the count to every tag. Some basic information will display on the PHP file page, wait for the PHP file to completely load and then it should be done.

voila! You’re done. Check your WordPress and you’ll notice the counts will work now. This script works with PHP7 and Comments in WordPress as well, just run this script.

<?php
include("wp-config.php");

$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD)) {  die('Could not connect: ' . mysqli_error());  }
if (!mysqli_select_db($conn,DB_NAME)) {  die('Could not connect: ' . mysqli_error());  }

$result = mysqli_query($conn, "SELECT term_taxonomy_id FROM ".$table_prefix."term_taxonomy");
while ($row = mysqli_fetch_array($result)) {
  $term_taxonomy_id = $row['term_taxonomy_id'];
  echo "term_taxonomy_id: ".$term_taxonomy_id." count = ";
  $countresult = mysqli_query($conn, "SELECT count(*) FROM ".$table_prefix."term_relationships WHERE term_taxonomy_id = '$term_taxonomy_id'");
  $countarray = mysqli_fetch_array($countresult);
  $count = $countarray[0];
  echo $count."
";
 mysqli_query($conn, "UPDATE ".$table_prefix."term_taxonomy SET count = '$count' WHERE term_taxonomy_id = '$term_taxonomy_id'");
		}

$result = mysqli_query($conn, "SELECT ID FROM ".$table_prefix."posts");
while ($row = mysqli_fetch_array($result)) {
  $post_id = $row['ID'];
  echo "post_id: ".$post_id." count = ";
  $countresult = mysqli_query($conn, "SELECT count(*) FROM ".$table_prefix."comments WHERE comment_post_ID = '$post_id' AND comment_approved = 1");
  $countarray = mysqli_fetch_array($countresult);
  $count = $countarray[0];
  echo $count."
";
  mysqli_query("UPDATE ".$table_prefix."posts SET comment_count = '$count' WHERE ID = '$post_id'");
		}
?>

12 thoughts to “WordPress taxonomy terms and comments count showing zero after import”

  1. Thank you so much! My taxonomies count suddenly went to 0. I was going crazy rolling back plugin updates, to no avail. This fixed it in a second!

  2. This is great, thank you. One note: Line 32 throws an error. “Warning: mysqli_query() expects at least 2 parameters, 1 given”. It’s missing the first argument, which I believe should be the $conn variable.

  3. One more thank you! A version that does not connect with the database under PHP 7 is still being passed along. Glad I found this. Now I can go learn the difference between mysql and myslqi.

  4. The script doesn’t work too well under the more syntactically strict PHP 8, so some corrections:

    1. include(“wp-config.php”) in line 3:
    get rid of the brackets: include ‘wp-config.php’;

    2. mysqli_query(“UPDATE “.$table_prefix.”posts SET comment_count = ‘$count’ WHERE ID = ‘$post_id'”) in line 31:
    you missed the first argument while you did it correctly in the lines further above: add the $conn string like this
    mysqli_query( $conn, “UPDATE ” . $table_prefix . “posts SET comment_count = ‘” . $count . “‘ WHERE ID = ‘” . $post_id . “‘” );

    3. General rule of thumb: take the variables out of the double quotation marked query strings like you did with the table prefix strings:
    “SELECT count(*) FROM ” . $table_prefix .”term_relationships WHERE term_taxonomy_id = ‘” . $term_taxonomy_id . “‘”
    Do so for each query string in the code.

    After that this valuable snippet should run just fine under PHP 8

Leave a Reply