Make WordPress Core

Changeset 6079


Ignore:
Timestamp:
09/11/2007 07:33:35 PM (19 years ago)
Author:
ryan
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/import/wp-cat2tag.php

    r5980 r6079  
    127127                                }
    128128
    129                                 // Set the category itself to $type from above
    130                                 $wpdb->query("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'");
    131 
    132                                 // Set all parents to 0 (root-level) if their parent was the converted tag
    133                                 $wpdb->query("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = '{$category->term_id}' AND taxonomy = 'category'");
    134 
     129                                // If the category is the default, leave category in place and create tag.
     130                                if ( get_option('default_category') == $category->term_id ) {
     131                                        $id = wp_insert_term($category->name, 'post_tag', array('slug' => $category->slug));
     132                                        $id = $id['term_taxonomy_id'];
     133                                        $posts = get_objects_in_term($category->term_id, 'category');
     134                                        foreach ( $posts as $post ) {
     135                                                if ( !$wpdb->get_var("SELECT object_id FROM $wpdb->term_relationships WHERE object_id = '$post' AND term_taxonomy_id = '$id'") )                                               
     136                                                        $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$post', '$id')");
     137                                        }
     138                                } else {
     139                                        // Change the category to a tag.
     140                                        $wpdb->query("UPDATE $wpdb->term_taxonomy SET taxonomy = 'post_tag' WHERE term_id = '{$category->term_id}' AND taxonomy = 'category'");
     141
     142                                        // Set all parents to 0 (root-level) if their parent was the converted tag
     143                                        $wpdb->query("UPDATE $wpdb->term_taxonomy SET parent = 0 WHERE parent = '{$category->term_id}' AND taxonomy = 'category'");
     144                                }
    135145                                // Clean the cache
    136146                                clean_category_cache($category->term_id);
Note: See TracChangeset for help on using the changeset viewer.