wp_revisions_to_keep()
云策文档标注
概述
wp_revisions_to_keep() 函数用于确定给定文章应保留的修订版本数量。它基于 WP_POST_REVISIONS 常量、文章类型支持和过滤器来动态计算返回值。
关键要点
- 默认情况下,WordPress 保留无限数量的修订版本,但可通过 WP_POST_REVISIONS 常量在 wp-config.php 中设置限制。
- 函数接受一个 WP_Post 对象作为参数,返回一个整数表示应保留的修订数量。
- 如果文章类型不支持修订(通过 post_type_supports() 检查),则返回 0。
- 提供两个过滤器:'wp_revisions_to_keep' 用于通用覆盖,以及动态钩子如 'wp_post_revisions_to_keep' 用于按文章类型覆盖。
代码示例
function wpdocs_myplugin_admin_notices() {
global $post;
$revisions = wp_get_post_revisions( $post->ID );
if ( isset( $post ) && wp_revisions_to_keep( $post )
注意事项
- WP_POST_REVISIONS 可设置为 true(无限)、false(禁用)或整数(具体数量)。
- 过滤器优先级:动态文章类型过滤器覆盖通用过滤器,两者都覆盖 WP_POST_REVISIONS 常量。
- 函数在 WordPress 3.6.0 版本引入,常用于 wp_revisions_enabled() 和 wp_save_post_revision() 等函数中。
原文内容
Determines how many revisions to retain for a given post.
Description
By default, an infinite number of revisions are kept.
The constant WP_POST_REVISIONS can be set in wp-config to specify the limit of revisions to keep.
Parameters
$postWP_Postrequired-
The post object.
Source
function wp_revisions_to_keep( $post ) {
$num = WP_POST_REVISIONS;
if ( true === $num ) {
$num = -1;
} else {
$num = (int) $num;
}
if ( ! post_type_supports( $post->post_type, 'revisions' ) ) {
$num = 0;
}
/**
* Filters the number of revisions to save for the given post.
*
* Overrides the value of WP_POST_REVISIONS.
*
* @since 3.6.0
*
* @param int $num Number of revisions to store.
* @param WP_Post $post Post object.
*/
$num = apply_filters( 'wp_revisions_to_keep', $num, $post );
/**
* Filters the number of revisions to save for the given post by its post type.
*
* Overrides both the value of WP_POST_REVISIONS and the 'wp_revisions_to_keep' filter.
*
* The dynamic portion of the hook name, `$post->post_type`, refers to
* the post type slug.
*
* Possible hook names include:
*
* - `wp_post_revisions_to_keep`
* - `wp_page_revisions_to_keep`
*
* @since 5.8.0
*
* @param int $num Number of revisions to store.
* @param WP_Post $post Post object.
*/
$num = apply_filters( "wp_{$post->post_type}_revisions_to_keep", $num, $post );
return (int) $num;
}
Hooks
- apply_filters( ‘wp_revisions_to_keep’, int $num, WP_Post $post )
-
Filters the number of revisions to save for the given post.
- apply_filters( “wp_{$post->post_type}_revisions_to_keep”, int $num, WP_Post $post )
-
Filters the number of revisions to save for the given post by its post type.
Changelog
| Version | Description |
|---|---|
| 3.6.0 | Introduced. |
Skip to note 2 content
Codex
Example
This code notifies a user when the post they are working has reached the limit defined by wp_revisions_to_keep