Skip to content

Commit a689c33

Browse files
authored
Fix: allow wp post delete to trash custom post types (#583)
1 parent 4b8c4cc commit a689c33

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

features/post.feature

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,29 @@ Feature: Manage WordPress posts
4141
Success: Deleted post {POST_ID}.
4242
"""
4343

44-
When I try `wp post delete {CUSTOM_POST_ID}`
45-
Then STDERR should be:
44+
When I run `wp post delete {CUSTOM_POST_ID}`
45+
Then STDOUT should be:
4646
"""
47-
Warning: Posts of type 'test' do not support being sent to trash.
48-
Please use the --force flag to skip trash and delete them permanently.
47+
Success: Trashed post {CUSTOM_POST_ID}.
4948
"""
5049

51-
When I run `wp post delete {CUSTOM_POST_ID} --force`
50+
When I run the previous command again
5251
Then STDOUT should be:
5352
"""
5453
Success: Deleted post {CUSTOM_POST_ID}.
5554
"""
5655

56+
Scenario: Force-deleting a custom post type post skips trash
57+
When I run `wp post create --post_title='Test CPT post' --post_type='book' --porcelain`
58+
Then STDOUT should be a number
59+
And save STDOUT as {BOOK_POST_ID}
60+
61+
When I run `wp post delete {BOOK_POST_ID} --force`
62+
Then STDOUT should be:
63+
"""
64+
Success: Deleted post {BOOK_POST_ID}.
65+
"""
66+
5767
When I try the previous command again
5868
Then the return code should be 1
5969

src/Post_Command.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -506,23 +506,24 @@ protected function delete_callback( $post_id, $assoc_args ) {
506506
$status = get_post_status( $post_id );
507507
$post_type = get_post_type( $post_id );
508508

509-
if ( ! $assoc_args['force']
510-
&& 'trash' !== $status
511-
&& ( 'post' !== $post_type && 'page' !== $post_type ) ) {
512-
return [
513-
'error',
514-
"Posts of type '{$post_type}' do not support being sent to trash.\n"
515-
. 'Please use the --force flag to skip trash and delete them permanently.',
516-
];
517-
}
509+
$force_delete = $assoc_args['force'] || 'trash' === $status || 'revision' === $post_type;
518510

519-
if ( ! wp_delete_post( $post_id, $assoc_args['force'] ) ) {
520-
return [ 'error', "Failed deleting post {$post_id}." ];
511+
if ( $force_delete || ! EMPTY_TRASH_DAYS ) {
512+
if ( ! wp_delete_post( $post_id, true ) ) {
513+
return [ 'error', "Failed deleting post {$post_id}." ];
514+
}
515+
516+
return [ 'success', "Deleted post {$post_id}." ];
521517
}
522518

523-
$action = $assoc_args['force'] || 'trash' === $status || 'revision' === $post_type ? 'Deleted' : 'Trashed';
519+
// Use wp_trash_post() directly because wp_delete_post() only auto-trashes
520+
// 'post' and 'page' types, permanently deleting all other post types even
521+
// when $force_delete is false. wp_trash_post() works for all post types.
522+
if ( wp_trash_post( $post_id ) ) {
523+
return [ 'success', "Trashed post {$post_id}." ];
524+
}
524525

525-
return [ 'success', "{$action} post {$post_id}." ];
526+
return [ 'error', "Failed trashing post {$post_id}." ];
526527
}
527528

528529
/**

0 commit comments

Comments
 (0)