钩子文档

wp_untrash_post_status

💡 云策文档标注

概述

wp_untrash_post_status 是一个 WordPress 过滤器,用于控制文章从回收站恢复时被分配的状态。默认情况下,恢复的文章状态为 'draft',但开发者可以通过此过滤器自定义状态,例如恢复文章到其被删除前的原始状态。

关键要点

  • 过滤器名称:wp_untrash_post_status,用于在文章从回收站恢复时修改其状态。
  • 默认行为:恢复的文章状态为 'draft',但可以通过返回 $previous_status 来恢复原始状态。
  • 参数:$new_status(新状态)、$post_id(文章ID)、$previous_status(删除前的状态)。
  • 相关函数:wp_untrash_post_set_previous_status() 可用于恢复原始状态。
  • 版本变化:WordPress 5.6.0 引入此过滤器,之前版本恢复文章总是分配原始状态。

代码示例

$post_status = apply_filters( 'wp_untrash_post_status', $new_status, $post_id, $previous_status );

注意事项

  • 在 WordPress 5.6.0 之前,恢复的文章总是分配其原始状态,之后版本默认改为 'draft',需使用此过滤器进行自定义。
  • 使用 wp_untrash_post_set_previous_status() 函数可以方便地恢复文章到删除前的状态。

📄 原文内容

Filters the status that a post gets assigned when it is restored from the trash (untrashed).

Description

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.

Parameters

$new_statusstring
The new status of the post being restored.
$post_idint
The ID of the post being restored.
$previous_statusstring
The status of the post at the point where it was trashed.

Source

$post_status = apply_filters( 'wp_untrash_post_status', $new_status, $post_id, $previous_status );

Changelog

Version Description
5.6.0 Introduced.