Skip to content

Error out on post term set/add when no term ID exists - #692

Open
dhrupo wants to merge 1 commit into
wp-cli:mainfrom
dhrupo:fix/post-term-set-invalid-id
Open

dhrupo wants to merge 1 commit into
wp-cli:mainfrom
dhrupo:fix/post-term-set-invalid-id

Conversation

@dhrupo

@dhrupo dhrupo commented Nov 6, 2026 •

Copy link
Copy Markdown

Fixes #690

wp post term set <id> <taxonomy> <term-id> --by=id removes every term from the object when none of the given IDs exist, and still reports success:

$ wp post term add 1 post_tag apple banana
Success: Added terms.
$ wp post term set 1 post_tag 9999 --by=id
Success: Set term.
$ wp post term list 1 post_tag --format=count
0

prepare_terms() drops IDs that do not match a term. When none match, set() passes an empty array to wp_set_object_terms() with $append = false, which removes all existing terms. add --by=id takes the same path: it adds nothing and prints Success: Added term..

set and add now stop with Error: Could not find any term with the given ID. (or IDs) when --by=id resolves to no terms, before anything is written. If at least one ID exists, the behavior is unchanged. remove --by=id already errors on this case (Please specify one or more terms, or use --all.), so it is left as is. The change also covers wp user term set|add, which share CommandWithTerms.

Testing

Adds a scenario to features/post-term.feature, after "Add terms by term id". It adds two tags to a post, runs set --by=id with one missing ID and add --by=id with two, checks both fail with exit code 1 and an empty STDOUT, then checks both tags are still attached.

  • Without the fix, the scenario fails because wp post term set 1 post_tag 9999 --by=id prints Success: Set term. with exit status 0, and the post is left with 0 tags (WordPress 7.1.4, MySQL 8.4).
  • With the fix, features/post-term.feature (7 scenarios) and features/user-term.feature (3 scenarios) pass on WordPress 7.1.4, MySQL 8.4.
  • PHPCS passes on src/WP_CLI/CommandWithTerms.php. The linter and Gherkin lint pass.

AI assistance: I used Claude Code to find this and write the change; I reviewed and ran everything myself.

Summary by CodeRabbit

  • Bug Fixes
    • Commands for adding or setting terms by ID now report an error when the specified IDs don’t match existing terms, rather than completing without a clear failure. Existing terms remain unchanged.

@dhrupo
dhrupo requested a review from a team as a code owner November 6, 2026 18:12
@coderabbitai

coderabbitai Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

When --by is supplied and term preparation returns no terms, the add and set commands now report an error. A feature scenario checks the error output and confirms that previously added terms remain listed.

Changes: Term ID validation

Layer / File(s) Summary
Handle empty ID-based term results
src/WP_CLI/CommandWithTerms.php, features/post-term.feature
The add and set commands report an error when --by is supplied and term preparation returns no terms. The error labels the input as ID or IDs based on the number of term arguments. The feature scenario checks the errors and verifies that previously added terms remain listed.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested labels: bug, scope:testing

Suggested reviewers: swissspidy

Merge Risk: Minimal · up to bfe3a

The commands reject unmatched term IDs before changing existing terms. No issue requiring a fix before merge was identified.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title describes the main change: reporting an error when no supplied post term ID exists.
Linked Issues check Passed [#690] CommandWithTerms::set() and add() stop before writing when no supplied IDs resolve. The shared implementation also covers user terms. The post-term scenario checks both errors and confirms …
Out of Scope Changes check Passed [#690] The shared command changes and post-term regression scenario address the linked issue. No unrelated changes are shown.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Linked Issues check

Explanation

[#690] CommandWithTerms::set() and add() stop before writing when no supplied IDs resolve. The shared implementation also covers user terms. The post-term scenario checks both errors and confirms that existing tags remain attached. Matching IDs retain the existing behavior.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 1 files. (1 skipped: 1 unsupported.)

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: dc4aa4f5-3276-4a6c-b512-68b3b01ac5e3
Commits

Reviewing files that changed from the base of the PR and between 47ff5a1 and bfe3ac3.

Files selected for processing: 2

  • features/post-term.feature
  • src/WP_CLI/CommandWithTerms.php

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@github-actions github-actions Bot added bug command:post-term-add Related to 'post term add' command command:post-term-set Related to 'post term set' command command:user-term-add Related to 'user term add' command command:user-term-set Related to 'user term set' command labels Nov 6, 2026
@codecov

codecov Bot commented Nov 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug command:post-term-add Related to 'post term add' command command:post-term-set Related to 'post term set' command command:user-term-add Related to 'user term add' command command:user-term-set Related to 'user term set' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wp post term set --by=id with a non-existent ID removes all terms and reports success

1 participant