Fix: allow wp post delete to trash custom post types#583
Fix: allow wp post delete to trash custom post types#583swissspidy merged 2 commits intowp-cli:mainfrom
wp post delete to trash custom post types#583Conversation
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 wp-cli#128.
|
Hello! 👋 Thanks for opening this pull request! Please check out our contributing guidelines. We appreciate you taking the initiative to contribute to this project. Contributing isn't limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation. Here are some useful Composer commands to get you started:
To run a single Behat test, you can use the following command: # Run all tests in a single file
composer behat features/some-feature.feature
# Run only a specific scenario (where 123 is the line number of the "Scenario:" title)
composer behat features/some-feature.feature:123You can find a list of all available Behat steps in our handbook. |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a long-standing limitation in Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses the issue of deleting custom post types by allowing them to be trashed by default, which is a great improvement. The implementation uses wp_trash_post() directly to bypass limitations in WordPress core, and the logic is sound. The Behat tests have been updated accordingly and a new scenario for force-deleting has been added, which is excellent. I have one suggestion to refactor the new logic in src/Post_Command.php to improve its readability and reduce some code duplication.
Combine the EMPTY_TRASH_DAYS check with the force-delete path to remove duplicated wp_delete_post() call.
There was a problem hiding this comment.
Pull request overview
Updates wp post delete to trash all post types (including custom/unregistered types) by default when trash is enabled, aligning behavior with user expectations and avoiding WordPress core’s wp_delete_post() limitation for non-post/page types.
Changes:
- Reworks
Post_Command::delete_callback()to usewp_trash_post()for the non-force trash path, andwp_delete_post( ..., true )for permanent deletion paths. - Updates Behat coverage to expect CPTs to be trashed first (then deleted on a second delete), and adds a scenario validating
--forceskips trash for CPTs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Post_Command.php | Changes deletion logic to trash CPTs by default via wp_trash_post(), preserving --force and “already trashed” permanent-delete behavior. |
| features/post.feature | Updates/extends Behat scenarios to reflect new CPT trash-then-delete behavior and verify --force behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
wp post delete <id>now trashes custom post types by default (same aspostandpage), instead of erroring with "Posts of type X do not support being sent to trash"wp_trash_post()directly for the trash path, bypassing thewp_delete_post()hardcodedpost/pagecheck in WordPress core (Trac #43672)--forcestill permanently deletes as beforeThe problem
wp_delete_post($id, false)only auto-trashespostandpagetypes — all other post types are permanently deleted even when$force_deleteisfalse. This is a WordPress core limitation (Trac #43672, open since 2018).The previous fix for #128 added an error message to warn users, but
wp_trash_post()works correctly for all post types. There's no reason to block trashing.The fix
Instead of checking post type and erroring,
delete_callbacknow:--force, already trashed, or revision) →wp_delete_post($id, true)→ "Deleted"EMPTY_TRASH_DAYSenabled) →wp_trash_post($id)→ "Trashed" (works for ALL post types)EMPTY_TRASH_DAYS === 0) →wp_delete_post($id, true)→ "Deleted"Testing
posttype)@require-sqlitefailure unrelated to this change)Closes #128.
AI disclosure: This PR was prepared with AI assistance (Claude Code). The diagnosis, fix, and tests were reviewed and validated by the submitter against a live WordPress multisite running MariaDB 10.11 with custom post types.