Skip to content

Commit 9da669f

Browse files
committed
command finished
1 parent ed07314 commit 9da669f

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

src/Term_Command.php

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -669,10 +669,11 @@ public function recount( $args ) {
669669
*
670670
* # Migrate a category's term (video) to tag taxonomy.
671671
* $ wp term migrate 9190
672-
* Taxonomy term `9190` for taxonomy `category` doesn't exist.
672+
* Post 219060 assigned to new term!
673+
* Old Term removed!
674+
* Success: Migration of `9190` term for 1 posts
673675
*/
674676
public function migrate( $args, $assoc_args ) {
675-
// Code based from https://wordpress.org/plugins/taxonomy-converter/
676677
global $wpdb;
677678
$clean_term_cache = $values = array();
678679
$term_reference = $args[0];
@@ -687,45 +688,41 @@ public function migrate( $args, $assoc_args ) {
687688

688689
$original_taxonomy = get_taxonomy( $original_taxonomy );
689690

690-
$id = wp_insert_term( $term->name, $destination_taxonomy, array( 'slug' => $term->slug ) );
691+
$id = wp_insert_term( $term->name, $destination_taxonomy, array( 'slug' => $term->slug, 'parent' => 0, 'description' => $term->description ) );
691692

692693
if ( is_wp_error( $id ) ) {
693-
WP_CLI::error( "An error has occured: " . $id->get_error_message() );
694+
WP_CLI::error( $id->get_error_message() );
694695
}
695696

696697
$posts = get_objects_in_term( $term->term_id, $original_taxonomy->name );
697698

698699
foreach ( $posts as $post ) {
699700
$type = get_post_type( $post );
700701
if ( in_array( $type, $original_taxonomy->object_type ) ) {
701-
$values[] = $wpdb->prepare( "(%d, %d, %d)", $post, $id['term_taxonomy_id'], 0 );
702+
$set_relationship = wp_set_object_terms( $post, $id['term_id'], $destination_taxonomy, true );
703+
704+
if ( is_wp_error( $set_relationship ) ) {
705+
WP_CLI::error( "On setting the relationship, " . $set_relationship->get_error_message() );
706+
}
707+
708+
WP_CLI::line( "Post {$post} assigned to new term!" );
702709
}
703710

704711
clean_post_cache( $post );
705712
}
706713

707-
if ( $values ) {
708-
$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)" );
709-
$wpdb->update($wpdb->term_taxonomy, array( 'count' => $term->count ), array( 'term_id' => $term->term_id, 'taxonomy' => $destination_taxonomy ) );
714+
clean_term_cache( $term->term_id );
710715

711-
WP_CLI::success( "Term migrated!" );
716+
WP_CLI::line( "Term migrated!" );
712717

713-
$clean_term_cache[] = $term->term_id;
714-
}
715-
716-
$del = wp_delete_term( $term->term_id, $original_taxonomy );
718+
$del = wp_delete_term( $term->term_id, $original_taxonomy->name );
717719

718720
if ( is_wp_error( $del ) ) {
719-
WP_CLI::error( "An error has occured: " . $del->get_error_message() );
721+
WP_CLI::error( "On deleting the term, " . $del->get_error_message() );
720722
}
721723

722-
// Set all parents to 0 (root-level) if their parent was the converted tag
723-
$wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 0 ), array( 'parent' => $term->term_id, 'taxonomy' => $destination_taxonomy ) );
724-
725-
if ( ! empty( $clean_term_cache ) ) {
726-
$clean_term_cache = array_unique( array_values( $clean_term_cache ) );
727-
clean_term_cache( $clean_term_cache, $destination_taxonomy );
728-
}
724+
WP_CLI::line( "Old Term removed!" );
725+
WP_CLI::success( "Migration of `{$term_reference}` term for " . count( $posts ) . " posts" );
729726
}
730727

731728
private function maybe_make_child() {

0 commit comments

Comments
 (0)