Skip to content

Commit 6e7e1ce

Browse files
committed
migrate to other parameters
1 parent 56c005e commit 6e7e1ce

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/Term_Command.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -644,34 +644,48 @@ public function recount( $args ) {
644644
* <term>
645645
* : Slug or ID of the term to migrate.
646646
*
647-
* <orig-taxonomy>
647+
* [--by=<field>]
648+
* : Explicitly handle the term value as a slug or id.
649+
* ---
650+
* default: id
651+
* options:
652+
* - slug
653+
* - id
654+
* ---
655+
*
656+
* [--from=<taxonomy>]
648657
* : Taxonomy slug of the term to migrate.
658+
* ---
659+
* default: category
660+
* ---
649661
*
650-
* <dest-taxonomy>
662+
* [--to=<taxonomy>]
651663
* : Taxonomy slug to migrate.
652664
* ---
665+
* default: post_tag
666+
* ---
653667
*
654668
* ## EXAMPLES
655669
*
656670
* # 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.
671+
* $ wp term migrate 9190
672+
* Taxonomy term `9190` for taxonomy `category` doesn't exist.
659673
*/
660674
public function migrate( $args, $assoc_args ) {
661675
// Code based from https://wordpress.org/plugins/taxonomy-converter/
662676
global $wpdb;
663677
$clean_term_cache = array();
664678
$term_reference = $args[0];
665-
$original_taxonomy = $args[1];
666-
$destination_taxonomy = $args[2];
667-
$exists = term_exists( $term_reference, $args[1] );
679+
$original_taxonomy = Utils\get_flag_value( $assoc_args, 'from' );
680+
$destination_taxonomy = Utils\get_flag_value( $assoc_args, 'to' );
681+
682+
$term = get_term_by( Utils\get_flag_value( $assoc_args, 'by' ), $term_reference, $original_taxonomy );
668683

669-
if ( ! empty( $exists ) ) {
684+
if ( ! empty( $term ) ) {
670685
WP_CLI::error( "Taxonomy term `{$term_reference}` for taxonomy `{$original_taxonomy}` doesn't exist." );
671686
}
672687

673-
$original_taxonomy = get_taxonomy( $term_reference );
674-
$term = get_term( $term_reference, $original_taxonomy );
688+
$original_taxonomy = get_taxonomy( $original_taxonomy );
675689

676690
$id = wp_insert_term($term->name, $destination_taxonomy, array( 'slug' => $term->slug ) );
677691

@@ -680,7 +694,7 @@ public function migrate( $args, $assoc_args ) {
680694
}
681695

682696
$id = $id['term_taxonomy_id'];
683-
$posts = get_objects_in_term( $term->term_id, $args[1] );
697+
$posts = get_objects_in_term( $term->term_id, $original_taxonomy );
684698

685699
foreach ( $posts as $post ) {
686700
$type = get_post_type( $post );
@@ -703,7 +717,7 @@ public function migrate( $args, $assoc_args ) {
703717
$del = wp_delete_term( $term_id, $tax );
704718

705719
if ( is_wp_error( $del ) ) {
706-
WP_CLI::error( "An error has occured: " . $id->get_error_message() );
720+
WP_CLI::error( "An error has occured: " . $del->get_error_message() );
707721
}
708722

709723
// Set all parents to 0 (root-level) if their parent was the converted tag

0 commit comments

Comments
 (0)