Skip to content

Fix wp term migrate skipping posts after post type migration#606

Merged
swissspidy merged 3 commits intomainfrom
copilot/fix-terms-migration-assignment-failures
Mar 22, 2026
Merged

Fix wp term migrate skipping posts after post type migration#606
swissspidy merged 3 commits intomainfrom
copilot/fix-terms-migration-assignment-failures

Conversation

Copy link
Contributor

Copilot AI commented Mar 22, 2026

When posts are migrated to a new post type before running wp term migrate, the command silently skips reassigning terms because it checks the post type against the source taxonomy's object_type — not the destination's. This causes term relationships to be lost entirely.

Changes

  • Core fix: Replace the source taxonomy object type check ($tax->object_type) with a destination taxonomy check ($dest_tax->object_type). Posts whose current type is registered with the destination taxonomy get the term assigned correctly.
  • Explicit validation: Fetch and validate $dest_tax upfront with a clear error if the destination taxonomy doesn't exist.
  • Warning instead of silent skip: Posts whose current type is not registered with the destination taxonomy now emit WP_CLI::warning() rather than being silently ignored.
  • Accurate post count: Success message now reflects only posts that were actually assigned the new term.

Example scenario now works

# Step 1: Migrate post type
for post_id in $(wp post list --post_type="post" --format=ids); do
  wp post update $post_id --post_type="news"
done

# Step 2: Migrate terms — previously silently skipped all posts
for term_id in $(wp term list category --format=ids); do
  wp term migrate $term_id --from=category --to=topic
done

New test scenarios

  • Term migrates correctly when posts have been moved to a different post type that supports the destination taxonomy.
  • Warning is emitted (and posts skipped) when the post's current type is not registered with the destination taxonomy.
Original prompt

This section details on the original issue you should resolve

<issue_title>Terms migration assignment fails when 2 differents post types</issue_title>
<issue_description>This is an exemple to enlight a missing behaviour or documentation improvement. I have posts with categories that I want to migrate to news with topics.

  1. I do a wp cli post edit to migrate the post type from posts to news: for post_id in $(wp post list --post_type="post" --format=ids); do wp post update $post_id --post_type="news"; done;
  2. I do a wp term migrate to migrate the terms from categories to topics: for term_id in $(wp term list category --format=ids); do wp term migrate $term_id --from=category --to=topic; done;

It results in not having anymore the news assigned to the topics 😢 Which is kind of normal since it would also require to change the relathionships.

What do you think about this?
Do you think it's a good idea to patch it or just add a warning in the documentation? or even both?

Anyway, I would be glad to contribute to this! 😃</issue_description>

Comments on the Issue (you are @copilot in this section)

@schlessera Yes, I think this should be a supported use case, probably using an additional flag or two.

Do you have a plan of attack for this already?</comment_new>
<comment_new>@schlessera
The above does not really guarantee that the target taxonomy does even apply to the CPT you've migrated the posts to. That's also something that WP-CLI can't really change, as the CPT is registered via code.

I think we're missing a piece of the logic here, and I'm not entirely sure we can properly solve this reliably.

Should the reassignment logic check the taxonomy<=>CPT relationship and throw an error if incorrect?</comment_new>


⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.

Copilot AI changed the title [WIP] Fix terms migration assignment when using different post types Fix wp term migrate skipping posts after post type migration Mar 22, 2026
Copilot AI requested a review from swissspidy March 22, 2026 00:21
@codecov
Copy link

codecov bot commented Mar 22, 2026

Codecov Report

❌ Patch coverage is 87.50000% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Term_Command.php 87.50% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy marked this pull request as ready for review March 22, 2026 08:46
@swissspidy swissspidy requested a review from a team as a code owner March 22, 2026 08:46
Copilot AI review requested due to automatic review settings March 22, 2026 08:46

This comment was marked as resolved.

@swissspidy swissspidy added this to the 2.8.8 milestone Mar 22, 2026
@swissspidy swissspidy merged commit 22bcd45 into main Mar 22, 2026
72 of 73 checks passed
@swissspidy swissspidy deleted the copilot/fix-terms-migration-assignment-failures branch March 22, 2026 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Terms migration assignment fails when 2 differents post types

3 participants