This utility moves Movable Type entry tags from Movable Type to WordPress. This was tested with MoveableType 3.3 and WordPress 2.3.
Before running this program, it is highly recommended that you backup your Wordpress database then run the mt3.3_to_wp2.3.php program which can be found here. The program will identify Movable Type entries that may not work correctly when moving from MovableType to WordPress. If there are any such entries found, correct them in WordPress before running this utility using a tool like phpMyAdmin. You should also have already taken action described in the WordPress documentation to force Wordpress to use the MovableType structure. Otherwise the tags in unmatched entries may not carry over to Wordpress because the utility will have no way to find the corresponding entry in WordPress.
In addition, if you have any tags named the same as your categories, you must first remove these tags from your Movable Type database before running this program. Wordpress 2.3 has this limitation because the slug must be unique in the wp_terms table. You can do this by looking for the Tags link on the Systems Shortcuts menu on the MT Main Menu. Then see if you have any tags with the same name as your categories. If you find any, delete them.
Here is an explanation of terms in the rightmost column.
Please read the above carefully! To actually run this program, add the argument ?run=1 to the end of the URL. Make sure you back up your Wordpress tables first in case of some catastrophic error occurs!
If after completion you wish to check whether your tag counts are correct use this SQL:
SELECT t.name, x.count AS 'Believed Tag Count' , count(*) AS 'Actual Tag Count' FROM wp_term_relationships r, wp_term_taxonomy x, wp_terms t WHERE r.term_taxonomy_id = x.term_taxonomy_id AND x.term_id = t.term_id AND taxonomy = 'post_tag' GROUP BY r.term_taxonomy_id ORDER BY 1
| MT Entry Title | MT Entry Name | MT Entry Creation Date | Linked in WP? | MT Entry Tags | WP Tag Action Added Updated None Needed |
|
|---|---|---|---|---|---|---|
| '. $row['entry_title'] . ' | ' . $row['entry_basename'] . ' | ' . $row['entry_created_display'] . ' | ' . $find_status . " | \n"; echo "" . implode($tag_array, ', ') . " | \n"; unset($tag_statuses); // This array is used to show the ACTION in Wordpress that was applied to the given tag from Movable Type if ($find_status == 'LINKED') // Found a corresponding post for the MT entry, so okay to process associated tags { foreach ($tag_array as $tag_row) // Process each tag for the entry, one per loop { // Create a slug for the new tag $slug = sanitize_file_name(substr($tag_row,0,55)); // Does the tag exist in WordPress? $sql2 = "SELECT *, t.term_id AS this_term_id FROM wp_terms t, wp_term_taxonomy x WHERE t.term_id = x.term_id AND slug = '" . $slug . "'"; $result2 = mysql_query($sql2); $exists_in_wp = false; while ($row2 = mysql_fetch_assoc($result2)) { $exists_in_wp = true; $this_term_id = $row2['this_term_id']; } if (!$exists_in_wp) { // Tag does not exist in WordPress, so create it. We italicize tags that will be created when they are first created $tag_statuses[] = '' . $tag_row . ''; // Insert the new tag $new_tag_name = str_replace("'", "\'", substr($tag_row,0,55)); $sql3 = "INSERT INTO wp_terms (name, slug) VALUES ('" . $new_tag_name . "', '" . $slug . "')"; $result3 = mysql_query($sql3) or exit("Unable to insert new tag into wp_terms table for a tag with a value of " . substr($tag_row,0,55) . ' SQL = ' . $sql3); // This should insert the new tag // Now that the new tag is in the database, create the corresponding row in the wp_term_taxonomy table // First, get the just created term_id $sql3 = 'SELECT MAX(term_id) AS max_term_id FROM wp_terms'; $result3 = mysql_query($sql3); $row3 = mysql_fetch_assoc($result3) or exit("Unable to select from wp_terms table for a tag with a value of " . substr($tag_row,0,55)); // This should insert the new tag $term_id = $row3['max_term_id']; // Now insert the new row in the wp_term_taxonomy table and give it an initial tag usage count of 1 $sql3 = 'INSERT INTO wp_term_taxonomy (term_id, taxonomy, description, count) VALUES (' . $term_id . ", 'post_tag', '', 1)"; $result3 = mysql_query($sql3) or exit("Unable to insert row in the wp_term_taxonomy table for the tag " . substr($tag_row,0,55)); // Get the newest term_taxonomy_id. We will need it to create the term relationship $sql3 = 'SELECT MAX(term_taxonomy_id) AS max_term_taxonomy_id FROM wp_term_taxonomy'; $result3 = mysql_query($sql3); $row3 = mysql_fetch_assoc($result3) or exit("Unable to get the latest term_taxonomy_id"); $term_taxonomy_id = $row3['max_term_taxonomy_id']; // Create the term relationship $sql3 = 'INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES (' . $ID . ', ' . $term_taxonomy_id . ')'; $result3 = mysql_query($sql3) or exit("Unable to create a term relationship for the tag " . substr($tag_row,0,55) . ' SQL = ' . $sql3); // This should update the count } else { // Steps if the tag already exists: // -- See if this blog entry has a relationship with this tag (the term_relationship already exists) // -- If it does not: // -- Create term_relationship // -- Increment term taxonomy count // -- If it does assume all is well. No need to increment term taxonomy count. However, note in report that nothing was done // Get the term_taxonomy_id for the tag. Logically, the term relationship cannot exist if the term_taxonomy_id does not exist, // since first the term has to be defined. $term_relationship_exists = false; $term_taxonomy_id_exists = false; $tag_name = str_replace("'", "\'", substr($tag_row,0,55)); $sql3 = "SELECT term_taxonomy_id, t.term_id FROM wp_terms t, wp_term_taxonomy x WHERE t.term_id = x.term_id AND taxonomy = 'post_tag' AND name = '" . $tag_name . "'"; $result3 = mysql_query($sql3) or exit("Unable to get term_taxonomy_id, term_id for the tag " . substr($tag_row,0,55)); while ($row3 = mysql_fetch_assoc($result3)) { $term_taxonomy_id_exists = true; $term_taxonomy_id = $row3['term_taxonomy_id']; } // If the term_taxonomy_id exists, the term exists, but is there a relationship between the tag and this post in the wp_term_relationships table? if ($term_taxonomy_id_exists) { $sql3 = "SELECT count(*) AS relationship_count FROM wp_term_relationships WHERE object_id = " . $ID. " AND term_taxonomy_id = " . $term_taxonomy_id; $result3 = mysql_query($sql3) or exit("Unable to get term relationship for the tag " . substr($tag_row,0,55)); while ($row3 = mysql_fetch_assoc($result3)) { $relationship_count = $row3['relationship_count']; if ($relationship_count > 0) { $term_relationship_exists = true; } } } if (!$term_relationship_exists) { // Create the term relationship $sql3 = 'INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES (' . $ID . ', ' . $term_taxonomy_id . ')'; $result3 = mysql_query($sql3) or exit("Unable to create a term relationship for the tag " . substr($tag_row,0,55)); // This should update the count // Update the count for the number of times this tag is used $sql3 = 'UPDATE wp_term_taxonomy SET count = count + 1 WHERE term_id = ' . $this_term_id; $result3 = mysql_query($sql3) or exit("Unable to update row in the wp_term_taxonomy table for the tag " . substr($tag_row,0,55) . ' SQL = ' . $sql3); // This should update the count $tag_statuses[] = '' . $tag_row . ''; } else { // Annotate that all is in order by not marking up the tag in the column $tag_statuses[] = $tag_row; } } } // foreach $tag_array echo "" . implode($tag_statuses, ', ') . " | \n"; } else { echo "No tag action taken | \n"; } mysql_select_db($mt_db_name) or exit(); echo "