Skip to content

Set WP-CLI post categories by term ID, not slug#40

Merged
jeherve merged 1 commit into
m:mainfrom
jeherve:fix/wp-cli-set-post-categories-by-id
Jun 3, 2026
Merged

Set WP-CLI post categories by term ID, not slug#40
jeherve merged 1 commit into
m:mainfrom
jeherve:fix/wp-cli-set-post-categories-by-id

Conversation

@jeherve

@jeherve jeherve commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

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).

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 — green
  • ruff check lib/ tests/ — clean

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.
@jeherve jeherve self-assigned this Jun 3, 2026
@jeherve jeherve merged commit d2f0c04 into m:main Jun 3, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant