diff --git a/wp-admin/import/wp-cat2tag.php b/wp-admin/import/wp-cat2tag.php index 1c5d43a1c9..edbf4b5bd4 100644 --- a/wp-admin/import/wp-cat2tag.php +++ b/wp-admin/import/wp-cat2tag.php @@ -126,12 +126,22 @@ class WP_Categories_to_Tags { continue; } - // Set the category itself to $type from above - $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'"); + // If the category is the default, leave category in place and create tag. + if ( get_option('default_category') == $category->term_id ) { + $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug)); + $id = $id['term_taxonomy_id']; + $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_category_cache($category->term_id);