Skip to content

Commit 08f6b0b

Browse files
committed
first version, not tested yet
1 parent 5517499 commit 08f6b0b

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

src/Taxonomy_Command.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,81 @@ public function get( $args, $assoc_args ) {
195195
$formatter->display_item( $data );
196196
}
197197

198+
199+
200+
/**
201+
* Migrate a term of a taxonomy to another taxonomy.
202+
*
203+
* ## OPTIONS
204+
*
205+
* <term>
206+
* : Slug of the term to migrate.
207+
*
208+
* <orig-taxonomy>
209+
* : Taxonomy slug of the term to migrate.
210+
*
211+
* <dest-taxonomy>
212+
* : Taxonomy slug to migrate.
213+
* ---
214+
*
215+
* ## EXAMPLES
216+
*
217+
* # Migrate a category's term (video) to tag taxonomy.
218+
* $ wp taxonomy migrate video category post_tag
219+
* Term video has migrated from category taxonomy to post_tag taxonomy.
220+
*/
221+
public function migrate( $args, $assoc_args ) {
222+
// Code based from https://wordpress.org/plugins/taxonomy-converter/ plugin
223+
global $wpdb;
224+
$clean_term_cache = array();
225+
$exists = term_exists( $args[0], $args[1] );
226+
227+
if ( ! empty( $exists ) ) {
228+
WP_CLI::error( "Taxonomy {$args[0]} doesn't exist." );
229+
}
230+
231+
$original_taxonomy = get_taxonomy( $args[0] );
232+
$term = get_term( $args[0], $args[1] );
233+
234+
$id = wp_insert_term($term->name, $args[2], array( 'slug' => $term->slug ) );
235+
236+
if ( is_wp_error( $id ) ) {
237+
WP_CLI::error( "An error has occured: " . $id->get_error_message() );
238+
}
239+
240+
$id = $id['term_taxonomy_id'];
241+
$posts = get_objects_in_term( $term->term_id, $args[1] );
242+
$term_order = 0;
243+
244+
foreach ( $posts as $post ) {
245+
$type = get_post_type( $post );
246+
if ( in_array( $type, $original_taxonomy->object_type ) ) {
247+
$values[] = $wpdb->prepare( "(%d, %d, %d)", $post, $id, $term_order );
248+
}
249+
250+
clean_post_cache( $post );
251+
}
252+
253+
if ( $values ) {
254+
$wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)" );
255+
$wpdb->update($wpdb->term_taxonomy, array( 'count' => $term->count ), array( 'term_id' => $term->term_id, 'taxonomy' => $args[2] ) );
256+
257+
WP_CLI::success( "Term added to posts." );
258+
259+
$clean_term_cache[] = $term->term_id;
260+
}
261+
262+
$del = wp_delete_term( $term_id, $tax );
263+
264+
// Set all parents to 0 (root-level) if their parent was the converted tag
265+
$wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 0 ), array( 'parent' => $term_id, 'taxonomy' => $tax ) );
266+
267+
if ( ! empty( $clean_term_cache ) ) {
268+
$clean_term_cache = array_unique( array_values( $clean_term_cache ) );
269+
clean_term_cache ( $clean_term_cache, $new_tax );
270+
}
271+
}
272+
198273
private function get_formatter( &$assoc_args ) {
199274
return new \WP_CLI\Formatter( $assoc_args, $this->fields, 'taxonomy' );
200275
}

0 commit comments

Comments
 (0)