• 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'");
    		}
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter quarkin

    (@quarkin)

    Here is an update to this. Place this code in a php page and drop it in your root directory and then navigate to it to fix your comment and category counts if you have problems during imports. No changes need to be made to this code for it to work.

    <?php
    // Place this file in your root directory 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'");
    		}
    ?>

    AMAZING!

    You just saved my day, for sure.

    Did exactly as it should.

    Thanks!

    Thanks..
    It worked for me perfectly ??

    Adham

    fuddes

    (@fuddes)

    I’m getting the following errors:

    Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /[removed]/commentfix.php on line 2
    
    Parse error: syntax error, unexpected T_STRING in /[removed]/commentfix.php on line 2

    Anyone know how to fix this? There isn’t a “\” character in the script.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Fix comment and category counts after import’ is closed to new replies.