From 52e35999fba990a1679505c14f7f554a149fae8a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 22 Mar 2026 00:15:51 +0000 Subject: [PATCH 1/3] Initial plan From 49c00a6614dce487b71e549f372cb96d19a100ce Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 22 Mar 2026 00:21:01 +0000 Subject: [PATCH 2/3] Fix term migrate to check destination taxonomy's object types Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Agent-Logs-Url: https://github.com/wp-cli/entity-command/sessions/d99a043f-240a-40e9-a40d-e2b830de931a --- features/term-migrate.feature | 63 +++++++++++++++++++++++++++++++++++ src/Term_Command.php | 15 +++++++-- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/features/term-migrate.feature b/features/term-migrate.feature index d6c5e5c64..370086800 100644 --- a/features/term-migrate.feature +++ b/features/term-migrate.feature @@ -87,3 +87,66 @@ Feature: Migrate term custom fields """ Error: Taxonomy term 'peach' for taxonomy 'category' doesn't exist. """ + + @require-wp-4.4 + Scenario: Migrate a term when posts have been migrated to a different post type that supports the destination taxonomy + Given a WP install + And a wp-content/mu-plugins/test-migrate.php file: + """ + true ] ); + register_taxonomy( 'topic', 'news', [ 'public' => true ] ); + } ); + """ + + When I run `wp term create category grape` + Then STDOUT should not be empty + + When I run `wp post create --post_title='Test post' --post_type=post --porcelain` + Then STDOUT should be a number + And save STDOUT as {POST_ID} + + When I run `wp post term set {POST_ID} category grape` + Then STDOUT should not be empty + + When I run `wp post update {POST_ID} --post_type=news` + Then STDOUT should not be empty + + When I run `wp term migrate grape --by=slug --from=category --to=topic` + Then STDOUT should be: + """ + Term 'grape' assigned to post {POST_ID}. + Term 'grape' migrated. + Old instance of term 'grape' removed from its original taxonomy. + Success: Migrated the term 'grape' from taxonomy 'category' to taxonomy 'topic' for 1 post. + """ + + @require-wp-4.4 + Scenario: Migrate a term warns when post type is not registered with destination taxonomy + Given a WP install + + When I run `wp term create category grape` + Then STDOUT should not be empty + + When I run `wp post create --post_title='Test post' --post_type=post --porcelain` + Then STDOUT should be a number + And save STDOUT as {POST_ID} + + When I run `wp post term set {POST_ID} category grape` + Then STDOUT should not be empty + + When I run `wp post update {POST_ID} --post_type=page` + Then STDOUT should not be empty + + When I run `wp term migrate grape --by=slug --from=category --to=post_tag` + Then STDERR should contain: + """ + Warning: Term 'grape' not assigned to post {POST_ID}. Post type 'page' is not registered with taxonomy 'post_tag'. + """ + And STDOUT should contain: + """ + Success: Migrated the term 'grape' from taxonomy 'category' to taxonomy 'post_tag' for 0 posts. + """ diff --git a/src/Term_Command.php b/src/Term_Command.php index a3dbfac40..7559df1b2 100644 --- a/src/Term_Command.php +++ b/src/Term_Command.php @@ -833,6 +833,12 @@ public function migrate( $args, $assoc_args ) { WP_CLI::error( "Taxonomy '{$original_taxonomy}' doesn't exist." ); } + $dest_tax = get_taxonomy( $destination_taxonomy ); + + if ( ! $dest_tax ) { + WP_CLI::error( "Taxonomy '{$destination_taxonomy}' doesn't exist." ); + } + $id = wp_insert_term( $term->name, $destination_taxonomy, @@ -850,11 +856,12 @@ public function migrate( $args, $assoc_args ) { /** * @var string[] $post_ids */ - $post_ids = get_objects_in_term( $term->term_id, $tax->name ); + $post_ids = get_objects_in_term( $term->term_id, $tax->name ); + $post_count = 0; foreach ( $post_ids as $post_id ) { $type = get_post_type( (int) $post_id ); - if ( in_array( $type, $tax->object_type, true ) ) { + if ( in_array( $type, $dest_tax->object_type, true ) ) { $term_taxonomy_id = wp_set_object_terms( (int) $post_id, $id['term_id'], $destination_taxonomy, true ); if ( is_wp_error( $term_taxonomy_id ) ) { @@ -862,6 +869,9 @@ public function migrate( $args, $assoc_args ) { } WP_CLI::log( "Term '{$term->slug}' assigned to post {$post_id}." ); + ++$post_count; + } else { + WP_CLI::warning( "Term '{$term->slug}' not assigned to post {$post_id}. Post type '{$type}' is not registered with taxonomy '{$destination_taxonomy}'." ); } clean_post_cache( (int) $post_id ); @@ -878,7 +888,6 @@ public function migrate( $args, $assoc_args ) { } WP_CLI::log( "Old instance of term '{$term->slug}' removed from its original taxonomy." ); - $post_count = count( $post_ids ); $post_plural = Utils\pluralize( 'post', $post_count ); WP_CLI::success( "Migrated the term '{$term->slug}' from taxonomy '{$tax->name}' to taxonomy '{$destination_taxonomy}' for {$post_count} {$post_plural}." ); } From 5207ccc6fefb3f747afb3ca52a4d1f2a158b95f1 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sun, 22 Mar 2026 08:25:03 +0100 Subject: [PATCH 3/3] Apply suggestion from @swissspidy --- features/term-migrate.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/term-migrate.feature b/features/term-migrate.feature index 370086800..ea797c371 100644 --- a/features/term-migrate.feature +++ b/features/term-migrate.feature @@ -141,7 +141,7 @@ Feature: Migrate term custom fields When I run `wp post update {POST_ID} --post_type=page` Then STDOUT should not be empty - When I run `wp term migrate grape --by=slug --from=category --to=post_tag` + When I try `wp term migrate grape --by=slug --from=category --to=post_tag` Then STDERR should contain: """ Warning: Term 'grape' not assigned to post {POST_ID}. Post type 'page' is not registered with taxonomy 'post_tag'.