| 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 | } |
|---|