Set WP-CLI post categories by term ID, not slug#40
Merged
Conversation
set_post_categories comma-joined the term IDs into a single token and passed it to 'wp post term set' with no --by flag. wp defaults to matching by slug, so a numeric term ID like 390 was looked up as the slug '390'; when no such category existed, wp silently CREATED a junk category named '390' and assigned it to the post. Comma-joining also broke multi-category assignment (the whole '5,7' string was treated as one slug). Caught by a live test on a real site: set_post_categories(9082, [390]) produced a category named '390' instead of assigning term 390. Pass each term ID as a separate positional argument with --by=id. Add a regression test asserting the argv shape.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
set_post_categoriescomma-joined the term IDs into a single token and passed it towp post term setwith no--byflag. wp defaults to matching by slug, so a numeric term ID like 390 was looked up as the slug "390"; when no such category existed, wp silently created a junk category named "390" and assigned it to the post. Comma-joining also broke multi-category assignment (the whole "5,7" string was treated as one slug).I caught this with a live test on a real site:
set_post_categories(9082, [390])produced a category named "390" instead of assigning term 390.The fix passes each term ID as a separate positional argument with
--by=id. I added a regression test asserting the argv shape, and re-confirmed on the live site that assigning by ID now works without creating junk categories.Testing
python3 -m unittest discover tests— greenruff check lib/ tests/— clean