问题描述
double
它返回一个正确的 add_filter( 'wp_revisions_to_keep','limit_revisions',10,2 );
function limit_revisions( $num,$post ) {
$num = 2;
if ( get_the_modified_time( 'U',$post->ID ) + MONTH_IN_SECONDS >= current_time( 'U' ) ) {
$num = -1;
}
return $num;
}
值,但如果我更新旧帖子,它不会删除旧修订。
如果我从 $num
函数中删除 $post->ID
,它会按预期工作,但我不明白为什么。将 id 设置为该函数的参数会导致过滤器不起作用,这有什么问题?
解决方法
正如用户 Fresz 指出的,您不需要将 $num
传递给您的 limit_revisions
函数。
如果您查看 wp_revisions_to_keep
: https://developer.wordpress.org/reference/functions/wp_revisions_to_keep/ 的文档,您会注意到它需要一个参数:post
。
在您当前的设置中,您有 limit_revisions( $num,$post )
- 这意味着 $num
实际上是 post
- 因为您首先传递了它。
您可以将代码重构为:
add_filter( 'wp_revisions_to_keep','limit_revisions');
// Pass only post
function limit_revisions( $post ) {
$num = 2;
if ( get_the_modified_time( 'U',$post->ID ) + MONTH_IN_SECONDS >= current_time( 'U' ) ) {
$num = -1;
}
return $num;
}