#42030 closed enhancement (fixed)
Introduce pre_trash_post filter
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 4.9 | Priority: | normal |
| Severity: | normal | Version: | 4.9 |
| Component: | Posts, Post Types | Keywords: | has-patch |
| Focuses: | Cc: |
Description
In wp_delete_post we have this neat filter pre_delete_post whether to proceed with deletion. I was wondering why we don't have that for wp_trash_post, i.e. pre_trash_post.
It can cause some issues, for example in our extensions we're querying custom post types based on their trash status, and there are cases where we don't want to allow them to be trashed (e.g. they have another post type that links to the to-be-trashed one). However, once they're trashed, our queries based on their trash status are erroneous.
Another point that was made by Marius Jensen on #core:
I see no issue with introducing it to maintain parity with the delete feature (trashing is a soft delete after all)
Attachments (4)
Change History (17)
#3
@
8 years ago
- Milestone changed from Awaiting Review to 4.9
- Owner set to SergeyBiryukov
- Status changed from new to reviewing
#6
follow-up:
↓ 9
@
8 years ago
@SergeyBiryukov little nitpick, we use:
$post_trash = get_post( $id, ARRAY_A ); for wp_trash_post
and
$post_delete = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $id ) ); for wp_delete_post
Thus, we have type array in wp_trash_post, and type WP_Post in wp_delete_post.
I figured it would be easy to just improves the comment on the filter, but maybe even better to bring a little consistency between wp_trash_post and wp_delete_post.
If you think the proposed patch has a high impact, we can just go ahead and update the filter comments on wp_trash_post and wp_untrash_post to say array instead of WP_Post.
#9
in reply to:
↑ 6
@
8 years ago
Replying to bor0:
little nitpick, we use:
$post_trash = get_post( $id, ARRAY_A );forwp_trash_post
and
$post_delete = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $id ) );forwp_delete_post
Thus, we have type
arrayinwp_trash_post, and typeWP_Postinwp_delete_post.
Good catch. Luckily, wp_trash_post() already has WP_Post as one of the documented return values, as it falls back to wp_delete_post() if Trash is disabled. wp_untrash_post() also has WP_Post as a return value, which is not exactly accurate at the moment. Both function only pass the post ID to their hooks, not an array or object, so the internal representation should be relatively safe to change.
On a related note, wp_delete_post() has WP_Post as a documented return value, but returns a regular object instead. Same for pre_delete_post filter, $post parameter type is inaccurate there.
I think it makes sense to standardize on WP_Post here.
Replying to ramiy:
Why not using
pre_trash_{$post_type}?
This was meant to complement the existing pre_delete_post filter added in [34082]. There is no corresponding pre_delete_{$post_type} filter, it can be explored in a new ticket though.
@SergeyBiryukov can you triage this? Thanks!