钩子文档

wp_revisions_to_keep

💡 云策文档标注

概述

wp_revisions_to_keep 是一个 WordPress 过滤器,用于控制特定文章保留的修订版本数量。它覆盖 WP_POST_REVISIONS 常量,允许开发者根据文章类型自定义修订保留策略。

关键要点

  • 过滤器名称:wp_revisions_to_keep,用于修改文章保留的修订数量。
  • 参数:$num(整数,修订数量)和 $post(WP_Post 对象,文章对象)。
  • 默认行为:如果文章类型支持修订,默认保留无限数量的修订。
  • 回调函数必须返回处理后的 $num 值,否则修订可能为空。
  • 引入版本:WordPress 3.6.0。

代码示例

add_filter( 'wp_revisions_to_keep', 'filter_function_name', 10, 2 );

function filter_function_name( $num, $post ) {
    if( 'my_custom_post' == $post->post_type ) {
        $num = 5;
    }
    return $num;
}

注意事项

确保过滤器回调函数返回一个整数值,以避免修订数据丢失。


📄 原文内容

Filters the number of revisions to save for the given post.

Description

Overrides the value of WP_POST_REVISIONS.

Parameters

$numint
Number of revisions to store.
$postWP_Post
Post object.

More Information

  • By default, an infinite number of revisions are stored if a post type supports revisions.
  • Note that the filter callback functions must return a value after it is finished processing or the revisions will be empty.

Source

$num = apply_filters( 'wp_revisions_to_keep', $num, $post );

Changelog

Version Description
3.6.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example migrated from Codex:

    The example below sets the number of revisions for a theoretical post type ‘my_custom_post’.

    add_filter( 'wp_revisions_to_keep', 'filter_function_name', 10, 2 );
    
    function filter_function_name( $num, $post ) {
        
        if( 'my_custom_post' == $post->post_type ) {
            $num = 5;
        }
        return $num;
    }