Just today I was working on a clients WordPress install and ran into a considerable problem. I was trying to upgrade the version of WordPress from 2.1 to 2.6.2 and could imported the posts through the database import functionality (phpmyadmin). After doing the same with the comments, I thought all was set…Boy was I wrong…
The client had about 2,100 posts and everyone of them lot their category it was assigned to. If you run into this situation, simply do this and use my script and everything should be set without a problem!
1) Export wp_caterories and wp_post2cat from the old database through phpmyadmin (or similar)
2) Import these tables into your new WordPress install. These tables do not exist in the new WordPress, so nothing should be overwritten.
3) Backup and export your wp_term_relationships table from the new WordPress install (just in case).
4) Add your categories to your new WordPress install, making sure that the name remains absolutely identical. This means spaces and capital letters as well – You can always rename later.
5) Save the code blow as a PHP file and open it up in your browser.
<?php
global $adminemail, $baseurl;
//Enter your SQL Server HostName
$hostname=’localhost’;//Enter the Username to login to the SQL Server
$username=’wordpress_dbname’;//Enter the password to login to SQL Server – This is not seen in the code of a PHP page
$password=’wordpressdbpassword’;//Enter the name of the db you are connecting to
$dbname=’wordpressdb’;//Enter the email account of the system administrator
mysql_connect($hostname, $username, $password) or die(mysql_error()) ;
mysql_select_db($dbname) or die(mysql_error()) ;$postfind = “SELECT * FROM wp_post2cat”;
$post2cat = mysql_query($postfind);
$num_rows = mysql_num_rows($post2cat);
echo “Found $num_rows in the query.<br>”;
while ($row = mysql_fetch_array($post2cat)) {
$postid = $row[1];
$catid=$row[2];
$postfind5 = “SELECT * FROM `wp_categories` WHERE cat_ID=$catid”;
$post2cat5 = mysql_query($postfind5);
while ($row5 = mysql_fetch_array($post2cat5)) {
$catname = $row5[1];
}
$postfind2 = “SELECT * FROM `wp_terms` WHERE name=’$catname’”;
$post2cat2 = mysql_query($postfind2);
while ($row2 = mysql_fetch_array($post2cat2)) {
$termid = $row2[0];
$postfind4 = “SELECT * FROM `wp_term_taxonomy` WHERE term_id=’$termid’”;
$post2cat4 = mysql_query($postfind4);
while ($row4 = mysql_fetch_array($post2cat4)) {
$taxid = $row4[0];
$insertpost = “INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES (‘$postid’, ‘$taxid’)”;
$insertresults = mysql_query($insertpost);
}
}
}
?>
NOTE: You will need to change the settings in the above code to connect to your database. To find your settings, download wp-config.php from the root of your WordPress install and copy your info over.
6) After opening your php file of the above code, your imported posts should be categorized appropriately.
7) Be sure to delete your php file you saved from the code above.
You’re done! You can remov the wp_categories and wp_post2cat if you want to clean up your database, since these are not needed anymore.
Please note that this was 100% successful for me, but I cannot guarantee your success. Please do this at your own risk. I hope this post finds those who need it.







