From dc582d68d82c192931e6df83ceab4cd975ffff78 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 11 Sep 2007 19:33:35 +0000 Subject: [PATCH] Retain default category when converting categories to tags. fixes #4845 git-svn-id: https://develop.svn.wordpress.org/trunk@6079 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/import/wp-cat2tag.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) 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);