wp_untrash_post

The timeline below displays how wordpress function wp_untrash_post has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Restores a post from the Trash.
 *
 * @since 2.9.0
 * @since 5.6.0 An untrashed post is now returned to 'draft' status by default, except for
 *              attachments which are returned to their original 'inherit' status.
 *
 * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
 * @return WP_Post|false|null Post data on success, false or null on failure.
 */
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;
}

WordPress Version: 5.9

/**
 * Restores a post from the Trash.
 *
 * @since 2.9.0
 * @since 5.6.0 An untrashed post is now returned to 'draft' status by default, except for
 *              attachments which are returned to their original 'inherit' status.
 *
 * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
 * @return WP_Post|false|null Post data on success, false or null on failure.
 */
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 The `$previous_status` parameter was added.
     *
     * @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 The `$previous_status` parameter was added.
     *
     * @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 The `$previous_status` parameter was added.
     *
     * @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;
}

WordPress Version: 5.6

/**
 * Restores a post from the Trash.
 *
 * @since 2.9.0
 * @since 5.6.0 An untrashed post is now returned to 'draft' status by default, except for
 *              attachments which are returned to their original 'inherit' status.
 *
 * @param int $post_id Optional. Post ID. Default is ID of the global `$post`.
 * @return WP_Post|false|null Post data on success, false or null on failure.
 */
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 The `$previous_status` parameter was added.
     *
     * @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 The `$previous_status` parameter was added.
     *
     * @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 The `$previous_status` parameter was added.
     *
     * @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;
}

WordPress Version: 5.4

/**
 * Restore a post or page from the Trash.
 *
 * @since 2.9.0
 *
 * @param int $post_id Optional. Post ID. Default is ID of the global $post.
 * @return WP_Post|false|null Post data on success, false or null on failure.
 */
function wp_untrash_post($post_id = 0)
{
    $post = get_post($post_id);
    if (!$post) {
        return $post;
    }
    if ('trash' !== $post->post_status) {
        return false;
    }
    /**
     * Filters whether a post untrashing should take place.
     *
     * @since 4.9.0
     *
     * @param bool|null $untrash Whether to go forward with untrashing.
     * @param WP_Post   $post    Post object.
     */
    $check = apply_filters('pre_untrash_post', null, $post);
    if (null !== $check) {
        return $check;
    }
    /**
     * Fires before a post is restored from the Trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('untrash_post', $post_id);
    $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true);
    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
     *
     * @param int $post_id Post ID.
     */
    do_action('untrashed_post', $post_id);
    return $post;
}

WordPress Version: 5.3

/**
 * Restore a post or page from the Trash.
 *
 * @since 2.9.0
 *
 * @param int $post_id Optional. Post ID. Default is ID of the global $post.
 * @return WP_Post|false|null Post data on success, false or null on failure.
 */
function wp_untrash_post($post_id = 0)
{
    $post = get_post($post_id);
    if (!$post) {
        return $post;
    }
    if ('trash' !== $post->post_status) {
        return false;
    }
    /**
     * Filters whether a post untrashing should take place.
     *
     * @since 4.9.0
     *
     * @param bool|null $untrash Whether to go forward with untrashing.
     * @param WP_Post   $post    Post object.
     */
    $check = apply_filters('pre_untrash_post', null, $post);
    if (null !== $check) {
        return $check;
    }
    /**
     * Fires before a post is restored from the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('untrash_post', $post_id);
    $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true);
    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
     *
     * @param int $post_id Post ID.
     */
    do_action('untrashed_post', $post_id);
    return $post;
}

WordPress Version: 4.9

/**
 * Restore a post or page from the Trash.
 *
 * @since 2.9.0
 *
 * @param int $post_id Optional. Post ID. Default is ID of the global $post.
 * @return WP_Post|false|null Post data on success, false or null on failure.
 */
function wp_untrash_post($post_id = 0)
{
    $post = get_post($post_id);
    if (!$post) {
        return $post;
    }
    if ('trash' !== $post->post_status) {
        return false;
    }
    /**
     * Filters whether a post untrashing should take place.
     *
     * @since 4.9.0
     *
     * @param bool    $untrash Whether to go forward with untrashing.
     * @param WP_Post $post    Post object.
     */
    $check = apply_filters('pre_untrash_post', null, $post);
    if (null !== $check) {
        return $check;
    }
    /**
     * Fires before a post is restored from the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('untrash_post', $post_id);
    $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true);
    delete_post_meta($post_id, '_wp_trash_meta_status');
    delete_post_meta($post_id, '_wp_trash_meta_time');
    wp_update_post(array('ID' => $post_id, 'post_status' => $post_status));
    wp_untrash_post_comments($post_id);
    /**
     * Fires after a post is restored from the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('untrashed_post', $post_id);
    return $post;
}

WordPress Version: 4.4

/**
 * Restore a post or page from the Trash.
 *
 * @since 2.9.0
 *
 * @param int $post_id Optional. Post ID. Default is ID of the global $post.
 * @return WP_Post|false WP_Post object. False on failure.
 */
function wp_untrash_post($post_id = 0)
{
    if (!$post = get_post($post_id, ARRAY_A)) {
        return $post;
    }
    if ($post['post_status'] != 'trash') {
        return false;
    }
    /**
     * Fires before a post is restored from the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('untrash_post', $post_id);
    $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true);
    $post['post_status'] = $post_status;
    delete_post_meta($post_id, '_wp_trash_meta_status');
    delete_post_meta($post_id, '_wp_trash_meta_time');
    wp_insert_post(wp_slash($post));
    wp_untrash_post_comments($post_id);
    /**
     * Fires after a post is restored from the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('untrashed_post', $post_id);
    return $post;
}

WordPress Version: 4.3

/**
 * Restore a post or page from the Trash.
 *
 * @since 2.9.0
 *
 * @param int $post_id Optional. Post ID. Default is ID of the global $post.
 * @return WP_Post|false WP_Post object. False on failure.
 */
function wp_untrash_post($post_id = 0)
{
    if (!$post = get_post($post_id, ARRAY_A)) {
        return $post;
    }
    if ($post['post_status'] != 'trash') {
        return false;
    }
    /**
     * Fires before a post is restored from the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('untrash_post', $post_id);
    $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true);
    $post['post_status'] = $post_status;
    delete_post_meta($post_id, '_wp_trash_meta_status');
    delete_post_meta($post_id, '_wp_trash_meta_time');
    wp_insert_post($post);
    wp_untrash_post_comments($post_id);
    /**
     * Fires after a post is restored from the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('untrashed_post', $post_id);
    return $post;
}

WordPress Version: 4.0

/**
 * Restore a post or page from the Trash.
 *
 * @since 2.9.0
 *
 * @param int $post_id Optional. Post ID. Default is ID of the global $post.
 * @return WP_Post|bool WP_Post object. False on failure.
 */
function wp_untrash_post($post_id = 0)
{
    if (!$post = get_post($post_id, ARRAY_A)) {
        return $post;
    }
    if ($post['post_status'] != 'trash') {
        return false;
    }
    /**
     * Fires before a post is restored from the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('untrash_post', $post_id);
    $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true);
    $post['post_status'] = $post_status;
    delete_post_meta($post_id, '_wp_trash_meta_status');
    delete_post_meta($post_id, '_wp_trash_meta_time');
    wp_insert_post($post);
    wp_untrash_post_comments($post_id);
    /**
     * Fires after a post is restored from the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('untrashed_post', $post_id);
    return $post;
}

WordPress Version: 3.9

/**
 * Restores a post or page from the Trash
 *
 * @since 2.9.0
 *
 * @param int $post_id Post ID.
 * @return mixed False on failure
 */
function wp_untrash_post($post_id = 0)
{
    if (!$post = get_post($post_id, ARRAY_A)) {
        return $post;
    }
    if ($post['post_status'] != 'trash') {
        return false;
    }
    /**
     * Fires before a post is restored from the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('untrash_post', $post_id);
    $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true);
    $post['post_status'] = $post_status;
    delete_post_meta($post_id, '_wp_trash_meta_status');
    delete_post_meta($post_id, '_wp_trash_meta_time');
    wp_insert_post($post);
    wp_untrash_post_comments($post_id);
    /**
     * Fires after a post is restored from the trash.
     *
     * @since 2.9.0
     *
     * @param int $post_id Post ID.
     */
    do_action('untrashed_post', $post_id);
    return $post;
}

WordPress Version: 3.7

/**
 * Restores a post or page from the Trash
 *
 * @since 2.9.0
 * @uses do_action() on 'untrash_post' before undeletion
 * @uses do_action() on 'untrashed_post' after undeletion
 *
 * @param int $post_id Post ID.
 * @return mixed False on failure
 */
function wp_untrash_post($post_id = 0)
{
    if (!$post = get_post($post_id, ARRAY_A)) {
        return $post;
    }
    if ($post['post_status'] != 'trash') {
        return false;
    }
    do_action('untrash_post', $post_id);
    $post_status = get_post_meta($post_id, '_wp_trash_meta_status', true);
    $post['post_status'] = $post_status;
    delete_post_meta($post_id, '_wp_trash_meta_status');
    delete_post_meta($post_id, '_wp_trash_meta_time');
    wp_insert_post($post);
    wp_untrash_post_comments($post_id);
    do_action('untrashed_post', $post_id);
    return $post;
}