wp_trash_post()
Moves the specified post to the trash. If the trash is disabled, the post will be permanently deleted.
When moving a post to the trash, its status is set to trash. The previous status is saved in the meta-field _wp_trash_meta_status and the time of moving to the trash is recorded in the meta-field _wp_trash_meta_time. Also, all comments of the post receive the status post-trashed (in the comment_approve field), and the previous statuses of the post's comments are recorded in the post meta-field _wp_trash_meta_comments_status. All this is needed for restoring the post from the trash.
If, when calling this function, the constant EMPTY_TRASH_DAYS=false, control is fully passed to the function wp_delete_post().
It is always better to use this function instead of wp_delete_post(). However, wp_delete_post() passes control to wp_trash_post() if the trash is enabled and the post type is either post or page. For custom types, wp_delete_post() always deletes the post.
Trash Settings in WordPress
By default, the trash is enabled in WordPress and posts are stored in it for 30 days, after which they are permanently deleted. It can be disabled by setting (defining) the constant EMPTY_TRASH_DAYS=false. The constant should be defined in the configuration file wp-config.php:
define( 'EMPTY_TRASH_DAYS', false ); // trash is disabled define( 'EMPTY_TRASH_DAYS', true ); // trash is enabled. By default in WP define( 'EMPTY_TRASH_DAYS', 30 ); // trash is enabled and posts will be stored in it for 30 days, after which they will be deleted.
To disable the trash for specific post types, see the article: How to Disable Trash in WordPress
Use wp_untrash_post() when you need to programmatically restore a post from the trash.
Use wp_trash_comment() when you need to move a comment to the trash.
Hooks from the function
Returns
WP_Post|false|null. An array of post data or false.
Usage
wp_trash_post( $post_id );
- $post_id(integer) (required)
- ID of the post. By default, the value of the global variable $post is passed.
Examples
#1 Move the post to the trash
If the trash is enabled, we will move the post with ID=15 to the trash. The post will remain in the trash for 30 days, during this time we can restore it. After 30 days, the post will be deleted completely by WordPress.
$post_id = 15; wp_trash_post( $post_id );
If trash feature is not enabled, then wp_delete_post() function will be called.
Notes
- See: wp_delete_post()
Changelog
| Since 2.9.0 | Introduced. |