Skip to content

Fix: allow wp post delete to trash custom post types#583

Merged
swissspidy merged 2 commits intowp-cli:mainfrom
chubes4:fix/trash-custom-post-types
Mar 2, 2026
Merged

Fix: allow wp post delete to trash custom post types#583
swissspidy merged 2 commits intowp-cli:mainfrom
chubes4:fix/trash-custom-post-types

Conversation

@chubes4
Copy link
Contributor

@chubes4 chubes4 commented Feb 28, 2026

Summary

  • wp post delete <id> now trashes custom post types by default (same as post and page), instead of erroring with "Posts of type X do not support being sent to trash"
  • Uses wp_trash_post() directly for the trash path, bypassing the wp_delete_post() hardcoded post/page check in WordPress core (Trac #43672)
  • --force still permanently deletes as before

The problem

wp_delete_post($id, false) only auto-trashes post and page types — all other post types are permanently deleted even when $force_delete is false. 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_callback now:

  1. Force path (--force, already trashed, or revision) → wp_delete_post($id, true) → "Deleted"
  2. Trash path (EMPTY_TRASH_DAYS enabled) → wp_trash_post($id) → "Trashed" (works for ALL post types)
  3. Trash disabled (EMPTY_TRASH_DAYS === 0) → wp_delete_post($id, true) → "Deleted"

Testing

  • Updated existing behat scenario: CPT delete now expects "Trashed" then "Deleted" (same as post type)
  • Added new scenario: force-deleting a CPT skips trash
  • Existing scenario for already-trashed CPT unchanged
  • All 18 post.feature scenarios pass (1 pre-existing @require-sqlite failure 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.

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.
@chubes4 chubes4 requested a review from a team as a code owner February 28, 2026 21:11
@github-actions
Copy link
Contributor

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:

  • composer install: Install dependencies.
  • composer test: Run the full test suite.
  • composer phpcs: Check for code style violations.
  • composer phpcbf: Automatically fix code style violations.
  • composer phpunit: Run unit tests.
  • composer behat: Run behavior-driven tests.

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:123

You can find a list of all available Behat steps in our handbook.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 wp post delete where custom post types could not be sent to the trash by default, instead requiring a --force flag for permanent deletion or erroring out. The changes refactor the post deletion logic to ensure that custom post types are properly trashed when wp post delete is used without the --force flag, leveraging wp_trash_post() for consistent behavior across all post types. This improves the user experience by providing expected trashing functionality for custom post types.

Highlights

  • Custom Post Type Trashing: wp post delete now correctly trashes custom post types by default, aligning their behavior with standard posts and pages.
  • Direct wp_trash_post() usage: The implementation now directly utilizes wp_trash_post() for trashing, bypassing a WordPress core limitation where wp_delete_post() only auto-trashes 'post' and 'page' types.
  • --force behavior unchanged: The --force flag continues to permanently delete posts, skipping the trash, as before.
Changelog
  • features/post.feature
    • Updated an existing scenario to verify that custom post types are now trashed by default, then permanently deleted on a subsequent command.
    • Added a new scenario to confirm that force-deleting a custom post type correctly skips the trash.
  • src/Post_Command.php
    • Modified the delete_callback function to implement a new logic flow for post deletion.
    • Removed the previous error message that prevented trashing of custom post types.
    • Introduced direct calls to wp_trash_post() for non-forced deletions, ensuring custom post types are trashed.
    • Adjusted logic to handle cases where trash is disabled (EMPTY_TRASH_DAYS === 0) or when a post is already trashed/a revision.
Activity
  • The pull request 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.
  • All 18 post.feature scenarios passed, with one pre-existing @require-sqlite failure noted as unrelated to this change.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@swissspidy
Copy link
Member

@swissspidy swissspidy added the command:post-delete Related to 'post delete' command label Feb 28, 2026
@swissspidy swissspidy added this to the 2.9.0 milestone Feb 28, 2026
@swissspidy swissspidy requested a review from Copilot March 2, 2026 09:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 use wp_trash_post() for the non-force trash path, and wp_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 --force skips 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.

@swissspidy swissspidy merged commit a689c33 into wp-cli:main Mar 2, 2026
72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:post-delete Related to 'post delete' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wp post delete on custom post type

3 participants