Opened 5 years ago
Closed 5 years ago
#47773 closed defect (bug) (fixed)
wp_trash_post() ignores errors from wp_update_post()
Reported by: | siliconforks | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 5.3 | Priority: | normal |
Severity: | normal | Version: | 5.2.2 |
Component: | Posts, Post Types | Keywords: | has-patch |
Focuses: | Cc: |
Description
The documentation for wp_trash_post()
states that it returns "false or null on failure". It does indeed do that for some failure conditions, but it calls wp_update_post()
without checking its return value, so it just ignores any errors which occur there.
https://core.trac.wordpress.org/browser/tags/5.2.2/src/wp-includes/post.php?rev=45549#L2985
One of the consequences of this is that, in the admin section, if a user trashes a post and the wp_update_post()
call fails for some reason, the success message "1 post moved to the Trash" will be displayed even though the post did not actually get moved to the trash.
Attachments (2)
Change History (8)
#1
@
5 years ago
- Keywords needs-patch added
- Milestone changed from Awaiting Review to Future Release
#3
@
5 years ago
@abhijitrakas You could simply pass the second argument as false
to wp_update_post
to make it not return WP_Error
and then check only for the false value, like this
<?php $post_updated = wp_update_post( array( 'ID' => $post_id, 'post_status' => 'trash', ), false ); // Check if post was not updated. if ( ! $post_updated ) { return false; }
Also, there is wp_trash_post_comments
which could also fail.
#4
@
5 years ago
@manzoorwanijk No need to pass the second parameter as it's the default param in wp_update_post
function.
For wp_trash_post_comments
we can't stop the process. If we return false
on fail of wp_trash_post_comments
function, then the user might get confused as we return an error but the post is not there in the list as post status already changed.
Add check for post status update failure