Skip to content

Commit a63e7a3

Browse files
dhruposwissspidy
andauthored
Error out on post term set/add when no term ID exists (#692)
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
1 parent 834e286 commit a63e7a3

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎features/post-term.feature‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,41 @@ Feature: Manage post term
201201
| term_id | name | slug |
202202
| 3 | 4 | 4 |
203203

204+
Scenario: Set or add terms by a term id that does not exist
205+
Given a WP install
206+
207+
When I run `wp post term add 1 post_tag apple banana`
208+
Then STDOUT should be:
209+
"""
210+
Success: Added terms.
211+
"""
212+
213+
When I try `wp post term set 1 post_tag 9999 --by=id`
214+
Then STDERR should be:
215+
"""
216+
Error: Could not find any term with the given ID.
217+
"""
218+
And STDOUT should be empty
219+
And the return code should be 1
220+
221+
When I try `wp post term add 1 post_tag 9998 9999 --by=id`
222+
Then STDERR should be:
223+
"""
224+
Error: Could not find any term with the given IDs.
225+
"""
226+
And STDOUT should be empty
227+
And the return code should be 1
228+
229+
When I run `wp post term list 1 post_tag --field=slug`
230+
Then STDOUT should contain:
231+
"""
232+
apple
233+
"""
234+
And STDOUT should contain:
235+
"""
236+
banana
237+
"""
238+
204239
Scenario: Remove a term that is not assigned to the post
205240
Given a WP install
206241

‎src/WP_CLI/CommandWithTerms.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ public function add( $args, $assoc_args ) {
258258
$field = Utils\get_flag_value( $assoc_args, 'by' );
259259
if ( $field ) {
260260
$terms = $this->prepare_terms( $field, $terms, $taxonomy );
261+
if ( ! $terms ) {
262+
$id_label = Utils\pluralize( 'ID', count( $args ) );
263+
WP_CLI::error( "Could not find any term with the given {$id_label}." );
264+
}
261265
}
262266
$result = wp_set_object_terms( $object_id, $terms, $taxonomy, true );
263267

@@ -309,6 +313,10 @@ public function set( $args, $assoc_args ) {
309313
$field = Utils\get_flag_value( $assoc_args, 'by' );
310314
if ( $field ) {
311315
$terms = $this->prepare_terms( $field, $terms, $taxonomy );
316+
if ( ! $terms ) {
317+
$id_label = Utils\pluralize( 'ID', count( $args ) );
318+
WP_CLI::error( "Could not find any term with the given {$id_label}." );
319+
}
312320
}
313321
$result = wp_set_object_terms( $object_id, $terms, $taxonomy, false );
314322

0 commit comments

Comments
 (0)