Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions features/post-term.feature
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,41 @@ Feature: Manage post term
| term_id | name | slug |
| 3 | 4 | 4 |

Scenario: Set or add terms by a term id that does not exist
Given a WP install

When I run `wp post term add 1 post_tag apple banana`
Then STDOUT should be:
"""
Success: Added terms.
"""

When I try `wp post term set 1 post_tag 9999 --by=id`
Then STDERR should be:
"""
Error: Could not find any term with the given ID.
"""
And STDOUT should be empty
And the return code should be 1

When I try `wp post term add 1 post_tag 9998 9999 --by=id`
Then STDERR should be:
"""
Error: Could not find any term with the given IDs.
"""
And STDOUT should be empty
And the return code should be 1

When I run `wp post term list 1 post_tag --field=slug`
Then STDOUT should contain:
"""
apple
"""
And STDOUT should contain:
"""
banana
"""

Scenario: Remove all terms from post
Given a WP install

Expand Down
8 changes: 8 additions & 0 deletions src/WP_CLI/CommandWithTerms.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ public function add( $args, $assoc_args ) {
$field = Utils\get_flag_value( $assoc_args, 'by' );
if ( $field ) {
$terms = $this->prepare_terms( $field, $terms, $taxonomy );
if ( ! $terms ) {
$id_label = count( $args ) > 1 ? 'IDs' : 'ID';
WP_CLI::error( "Could not find any term with the given {$id_label}." );
}
}
$result = wp_set_object_terms( $object_id, $terms, $taxonomy, true );

Expand Down Expand Up @@ -309,6 +313,10 @@ public function set( $args, $assoc_args ) {
$field = Utils\get_flag_value( $assoc_args, 'by' );
if ( $field ) {
$terms = $this->prepare_terms( $field, $terms, $taxonomy );
if ( ! $terms ) {
$id_label = count( $args ) > 1 ? 'IDs' : 'ID';
WP_CLI::error( "Could not find any term with the given {$id_label}." );
}
}
$result = wp_set_object_terms( $object_id, $terms, $taxonomy, false );

Expand Down
Loading