Restores a post from the Trash.
Parameters
$post_id
intoptional- Post ID. Default is the ID of the global
$post
.
Source
function wp_untrash_post( $post_id = 0 ) {
$post = get_post( $post_id );
if ( ! $post ) {
return $post;
}
$post_id = $post->ID;
if ( 'trash' !== $post->post_status ) {
return false;
}
$previous_status = get_post_meta( $post_id, '_wp_trash_meta_status', true );
/**
* Filters whether a post untrashing should take place.
*
* @since 4.9.0
* @since 5.6.0 Added the `$previous_status` parameter.
*
* @param bool|null $untrash Whether to go forward with untrashing.
* @param WP_Post $post Post object.
* @param string $previous_status The status of the post at the point where it was trashed.
*/
$check = apply_filters( 'pre_untrash_post', null, $post, $previous_status );
if ( null !== $check ) {
return $check;
}
/**
* Fires before a post is restored from the Trash.
*
* @since 2.9.0
* @since 5.6.0 Added the `$previous_status` parameter.
*
* @param int $post_id Post ID.
* @param string $previous_status The status of the post at the point where it was trashed.
*/
do_action( 'untrash_post', $post_id, $previous_status );
$new_status = ( 'attachment' === $post->post_type ) ? 'inherit' : 'draft';
/**
* Filters the status that a post gets assigned when it is restored from the trash (untrashed).
*
* By default posts that are restored will be assigned a status of 'draft'. Return the value of `$previous_status`
* in order to assign the status that the post had before it was trashed. The `wp_untrash_post_set_previous_status()`
* function is available for this.
*
* Prior to WordPress 5.6.0, restored posts were always assigned their original status.
*
* @since 5.6.0
*
* @param string $new_status The new status of the post being restored.
* @param int $post_id The ID of the post being restored.
* @param string $previous_status The status of the post at the point where it was trashed.
*/
$post_status = apply_filters( 'wp_untrash_post_status', $new_status, $post_id, $previous_status );
delete_post_meta( $post_id, '_wp_trash_meta_status' );
delete_post_meta( $post_id, '_wp_trash_meta_time' );
$post_updated = wp_update_post(
array(
'ID' => $post_id,
'post_status' => $post_status,
)
);
if ( ! $post_updated ) {
return false;
}
wp_untrash_post_comments( $post_id );
/**
* Fires after a post is restored from the Trash.
*
* @since 2.9.0
* @since 5.6.0 Added the `$previous_status` parameter.
*
* @param int $post_id Post ID.
* @param string $previous_status The status of the post at the point where it was trashed.
*/
do_action( 'untrashed_post', $post_id, $previous_status );
return $post;
}
Hooks
- apply_filters( ‘pre_untrash_post’,
bool|null $untrash ,WP_Post $post ,string $previous_status ) Filters whether a post untrashing should take place.
- do_action( ‘untrashed_post’,
int $post_id ,string $previous_status ) Fires after a post is restored from the Trash.
- do_action( ‘untrash_post’,
int $post_id ,string $previous_status ) Fires before a post is restored from the Trash.
- apply_filters( ‘wp_untrash_post_status’,
string $new_status ,int $post_id ,string $previous_status ) Filters the status that a post gets assigned when it is restored from the trash (untrashed).
User Contributed Notes