Skip to content

Commit 317e976

Browse files
committed
Fix: allow wp post delete to trash custom post types
Use wp_trash_post() directly instead of wp_delete_post() for the trash path, because wp_delete_post() hardcodes auto-trash behavior to only 'post' and 'page' types (WordPress core #43672), permanently deleting all other post types even when $force_delete is false. wp_trash_post() works correctly for all post types, so we call it directly when --force is not set and EMPTY_TRASH_DAYS is enabled. Previously, `wp post delete` on a custom post type would error: 'Posts of type X do not support being sent to trash.' Now it trashes the post like it does for 'post' and 'page' types. Closes #128.
1 parent defb5ea commit 317e976

2 files changed

Lines changed: 35 additions & 17 deletions

File tree

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: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -506,23 +506,31 @@ 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-
];
509+
if ( $assoc_args['force'] || 'trash' === $status || 'revision' === $post_type ) {
510+
if ( ! wp_delete_post( $post_id, true ) ) {
511+
return [ 'error', "Failed deleting post {$post_id}." ];
512+
}
513+
514+
return [ 'success', "Deleted post {$post_id}." ];
517515
}
518516

519-
if ( ! wp_delete_post( $post_id, $assoc_args['force'] ) ) {
520-
return [ 'error', "Failed deleting post {$post_id}." ];
517+
// Use wp_trash_post() directly because wp_delete_post() only auto-trashes
518+
// 'post' and 'page' types, permanently deleting all other post types even
519+
// when $force_delete is false. wp_trash_post() works for all post types.
520+
if ( EMPTY_TRASH_DAYS && wp_trash_post( $post_id ) ) {
521+
return [ 'success', "Trashed post {$post_id}." ];
521522
}
522523

523-
$action = $assoc_args['force'] || 'trash' === $status || 'revision' === $post_type ? 'Deleted' : 'Trashed';
524+
// Trash is disabled via EMPTY_TRASH_DAYS, or wp_trash_post() failed.
525+
if ( ! EMPTY_TRASH_DAYS ) {
526+
if ( ! wp_delete_post( $post_id, true ) ) {
527+
return [ 'error', "Failed deleting post {$post_id}." ];
528+
}
529+
530+
return [ 'success', "Deleted post {$post_id}." ];
531+
}
524532

525-
return [ 'success', "{$action} post {$post_id}." ];
533+
return [ 'error', "Failed trashing post {$post_id}." ];
526534
}
527535

528536
/**

0 commit comments

Comments
 (0)