Retain default category when converting categories to tags. fixes #4845

git-svn-id: http://svn.automattic.com/wordpress/trunk@6079 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-09-11 19:33:35 +00:00
parent c3155a54b2
commit 33953bc337
1 changed files with 15 additions and 5 deletions

View File

@ -126,12 +126,22 @@ class WP_Categories_to_Tags {
continue; continue;
} }
// Set the category itself to $type from above // If the category is the default, leave category in place and create tag.
$wpdb->query("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'"); if ( get_option('default_category') == $category->term_id ) {
$id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug));
// Set all parents to 0 (root-level) if their parent was the converted tag $id = $id['term_taxonomy_id'];
$wpdb->query("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = '{$category->term_id}' AND taxonomy = 'category'"); $posts = get_objects_in_term($category->term_id, 'category');
foreach ( $posts as $post ) {
if ( !$wpdb->get_var("SELECT object_id FROM $wpdb->term_relationships WHERE object_id = '$post' AND term_taxonomy_id = '$id'") )
$wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$post', '$id')");
}
} else {
// Change the category to a tag.
$wpdb->query("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'");
// Set all parents to 0 (root-level) if their parent was the converted tag
$wpdb->query("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = '{$category->term_id}' AND taxonomy = 'category'");
}
// Clean the cache // Clean the cache
clean_category_cache($category->term_id); clean_category_cache($category->term_id);