Skip to content

Commit 22bcd45

Browse files
Copilotswissspidy
andauthored
Fix wp term migrate skipping posts after post type migration (#606)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent 0dea732 commit 22bcd45

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

features/term-migrate.feature

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,66 @@ Feature: Migrate term custom fields
8787
"""
8888
Error: Taxonomy term 'peach' for taxonomy 'category' doesn't exist.
8989
"""
90+
91+
@require-wp-4.4
92+
Scenario: Migrate a term when posts have been migrated to a different post type that supports the destination taxonomy
93+
Given a WP install
94+
And a wp-content/mu-plugins/test-migrate.php file:
95+
"""
96+
<?php
97+
// Plugin Name: Test Migrate
98+
99+
add_action( 'init', function() {
100+
register_post_type( 'news', [ 'public' => true ] );
101+
register_taxonomy( 'topic', 'news', [ 'public' => true ] );
102+
} );
103+
"""
104+
105+
When I run `wp term create category grape`
106+
Then STDOUT should not be empty
107+
108+
When I run `wp post create --post_title='Test post' --post_type=post --porcelain`
109+
Then STDOUT should be a number
110+
And save STDOUT as {POST_ID}
111+
112+
When I run `wp post term set {POST_ID} category grape`
113+
Then STDOUT should not be empty
114+
115+
When I run `wp post update {POST_ID} --post_type=news`
116+
Then STDOUT should not be empty
117+
118+
When I run `wp term migrate grape --by=slug --from=category --to=topic`
119+
Then STDOUT should be:
120+
"""
121+
Term 'grape' assigned to post {POST_ID}.
122+
Term 'grape' migrated.
123+
Old instance of term 'grape' removed from its original taxonomy.
124+
Success: Migrated the term 'grape' from taxonomy 'category' to taxonomy 'topic' for 1 post.
125+
"""
126+
127+
@require-wp-4.4
128+
Scenario: Migrate a term warns when post type is not registered with destination taxonomy
129+
Given a WP install
130+
131+
When I run `wp term create category grape`
132+
Then STDOUT should not be empty
133+
134+
When I run `wp post create --post_title='Test post' --post_type=post --porcelain`
135+
Then STDOUT should be a number
136+
And save STDOUT as {POST_ID}
137+
138+
When I run `wp post term set {POST_ID} category grape`
139+
Then STDOUT should not be empty
140+
141+
When I run `wp post update {POST_ID} --post_type=page`
142+
Then STDOUT should not be empty
143+
144+
When I try `wp term migrate grape --by=slug --from=category --to=post_tag`
145+
Then STDERR should contain:
146+
"""
147+
Warning: Term 'grape' not assigned to post {POST_ID}. Post type 'page' is not registered with taxonomy 'post_tag'.
148+
"""
149+
And STDOUT should contain:
150+
"""
151+
Success: Migrated the term 'grape' from taxonomy 'category' to taxonomy 'post_tag' for 0 posts.
152+
"""

src/Term_Command.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,12 @@ public function migrate( $args, $assoc_args ) {
833833
WP_CLI::error( "Taxonomy '{$original_taxonomy}' doesn't exist." );
834834
}
835835

836+
$dest_tax = get_taxonomy( $destination_taxonomy );
837+
838+
if ( ! $dest_tax ) {
839+
WP_CLI::error( "Taxonomy '{$destination_taxonomy}' doesn't exist." );
840+
}
841+
836842
$id = wp_insert_term(
837843
$term->name,
838844
$destination_taxonomy,
@@ -850,18 +856,22 @@ public function migrate( $args, $assoc_args ) {
850856
/**
851857
* @var string[] $post_ids
852858
*/
853-
$post_ids = get_objects_in_term( $term->term_id, $tax->name );
859+
$post_ids = get_objects_in_term( $term->term_id, $tax->name );
860+
$post_count = 0;
854861

855862
foreach ( $post_ids as $post_id ) {
856863
$type = get_post_type( (int) $post_id );
857-
if ( in_array( $type, $tax->object_type, true ) ) {
864+
if ( in_array( $type, $dest_tax->object_type, true ) ) {
858865
$term_taxonomy_id = wp_set_object_terms( (int) $post_id, $id['term_id'], $destination_taxonomy, true );
859866

860867
if ( is_wp_error( $term_taxonomy_id ) ) {
861868
WP_CLI::error( "Failed to assign the term '{$term->slug}' to the post {$post_id}. Reason: " . $term_taxonomy_id->get_error_message() );
862869
}
863870

864871
WP_CLI::log( "Term '{$term->slug}' assigned to post {$post_id}." );
872+
++$post_count;
873+
} else {
874+
WP_CLI::warning( "Term '{$term->slug}' not assigned to post {$post_id}. Post type '{$type}' is not registered with taxonomy '{$destination_taxonomy}'." );
865875
}
866876

867877
clean_post_cache( (int) $post_id );
@@ -878,7 +888,6 @@ public function migrate( $args, $assoc_args ) {
878888
}
879889

880890
WP_CLI::log( "Old instance of term '{$term->slug}' removed from its original taxonomy." );
881-
$post_count = count( $post_ids );
882891
$post_plural = Utils\pluralize( 'post', $post_count );
883892
WP_CLI::success( "Migrated the term '{$term->slug}' from taxonomy '{$tax->name}' to taxonomy '{$destination_taxonomy}' for {$post_count} {$post_plural}." );
884893
}

0 commit comments

Comments
 (0)