函数文档

wp_untrash_post()

💡 云策文档标注

概述

wp_untrash_post() 函数用于从回收站恢复文章。它接受文章ID参数,返回恢复后的文章对象或失败状态,并涉及多个钩子和状态处理。

关键要点

  • 函数参数:$post_id(可选,默认使用全局$post的ID),返回值为WP_Post对象、false或null。
  • 恢复条件:仅当文章状态为'trash'时才能恢复,否则返回false。
  • 状态处理:默认恢复为'draft'状态(附件为'inherit'),可通过wp_untrash_post_status过滤器自定义。
  • 钩子:包括pre_untrash_post(过滤是否恢复)、untrash_post(恢复前触发)、untrashed_post(恢复后触发)。
  • 相关函数:如wp_update_post、wp_untrash_post_comments、delete_post_meta等。

代码示例

// Assume $post_id is the ID of the post you want to restore
$post_id = 123;

// Try to restore the post
$restored_post = wp_untrash_post( $post_id );

if ( false !== $restored_post ) {
    // The post was restored successfully, you can now do something with $restored_post
    echo 'Post restored successfully!';
} else {
    // Restore failed or post does not exist
    echo 'Failed to restore post or post does not exist.';
}

注意事项

  • 从WordPress 5.6.0开始,恢复的文章默认状态为'draft'(之前版本为原始状态),可通过过滤器调整。
  • 函数会删除与回收站相关的元数据(如_wp_trash_meta_status和_wp_trash_meta_time)。
  • 确保在恢复前检查文章是否存在和状态,以避免错误。

📄 原文内容

Restores a post from the Trash.

Parameters

$post_idintoptional
Post ID. Default is the ID of the global $post.

Return

WP_Post|false|null Post data on success, false or null on failure.

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).

Changelog

Version Description
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.
2.9.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    // Assume $post_id is the ID of the post you want to restore
    $post_id = 123;
    
    // Try to restore the post
    $restored_post = wp_untrash_post( $post_id );
    
    if ( false !== $restored_post ) {
        // The post was restored successfully, you can now do something with $restored_post
        echo 'Post restored successfully!';
    } else {
        // Restore failed or post does not exist
        echo 'Failed to restore post or post does not exist.';
    }