@@ -636,6 +636,85 @@ public function recount( $args ) {
636636 }
637637 }
638638
639+ /**
640+ * Migrate a term of a taxonomy to another taxonomy.
641+ *
642+ * ## OPTIONS
643+ *
644+ * <term>
645+ * : Slug or ID of the term to migrate.
646+ *
647+ * <orig-taxonomy>
648+ * : Taxonomy slug of the term to migrate.
649+ *
650+ * <dest-taxonomy>
651+ * : Taxonomy slug to migrate.
652+ * ---
653+ *
654+ * ## EXAMPLES
655+ *
656+ * # Migrate a category's term (video) to tag taxonomy.
657+ * $ wp taxonomy migrate video category post_tag
658+ * Term video has migrated from category taxonomy to post_tag taxonomy.
659+ */
660+ public function migrate ( $ args , $ assoc_args ) {
661+ // Code based from https://wordpress.org/plugins/taxonomy-converter/
662+ global $ wpdb ;
663+ $ clean_term_cache = array ();
664+ $ term_reference = $ args [0 ];
665+ $ original_taxonomy = $ args [1 ];
666+ $ destination_taxonomy = $ args [2 ];
667+ $ exists = term_exists ( $ term_reference , $ args [1 ] );
668+
669+ if ( ! empty ( $ exists ) ) {
670+ WP_CLI ::error ( "Taxonomy term ` {$ term_reference }` for taxonomy ` {$ original_taxonomy }` doesn't exist. " );
671+ }
672+
673+ $ original_taxonomy = get_taxonomy ( $ term_reference );
674+ $ term = get_term ( $ term_reference , $ original_taxonomy );
675+
676+ $ id = wp_insert_term ($ term ->name , $ destination_taxonomy , array ( 'slug ' => $ term ->slug ) );
677+
678+ if ( is_wp_error ( $ id ) ) {
679+ WP_CLI ::error ( "An error has occured: " . $ id ->get_error_message () );
680+ }
681+
682+ $ id = $ id ['term_taxonomy_id ' ];
683+ $ posts = get_objects_in_term ( $ term ->term_id , $ args [1 ] );
684+
685+ foreach ( $ posts as $ post ) {
686+ $ type = get_post_type ( $ post );
687+ if ( in_array ( $ type , $ original_taxonomy ->object_type ) ) {
688+ $ values [] = $ wpdb ->prepare ( "(%d, %d, %d) " , $ post , $ id , 0 );
689+ }
690+
691+ clean_post_cache ( $ post );
692+ }
693+
694+ if ( $ values ) {
695+ $ 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) " );
696+ $ wpdb ->update ($ wpdb ->term_taxonomy , array ( 'count ' => $ term ->count ), array ( 'term_id ' => $ term ->term_id , 'taxonomy ' => $ destination_taxonomy ) );
697+
698+ WP_CLI ::success ( "Term migrated! " );
699+
700+ $ clean_term_cache [] = $ term ->term_id ;
701+ }
702+
703+ $ del = wp_delete_term ( $ term_id , $ tax );
704+
705+ if ( is_wp_error ( $ del ) ) {
706+ WP_CLI ::error ( "An error has occured: " . $ id ->get_error_message () );
707+ }
708+
709+ // Set all parents to 0 (root-level) if their parent was the converted tag
710+ $ wpdb ->update ( $ wpdb ->term_taxonomy , array ( 'parent ' => 0 ), array ( 'parent ' => $ term_id , 'taxonomy ' => $ tax ) );
711+
712+ if ( ! empty ( $ clean_term_cache ) ) {
713+ $ clean_term_cache = array_unique ( array_values ( $ clean_term_cache ) );
714+ clean_term_cache ( $ clean_term_cache , $ args [2 ] );
715+ }
716+ }
717+
639718 private function maybe_make_child () {
640719 // 50% chance of making child term
641720 return ( mt_rand (1 , 2 ) == 1 );
0 commit comments