即时关闭对旧帖子的评论,无需任何额外的数据库查询。迷上了the_posts。
原型
_close_comments_for_old_posts( WP_Post $posts, WP_Query $query )
参数
$posts
(WP_Post)
(Required)
发布数据对象。
$query
(WP_Query)
(Required)
查询对象。
返回值
(array)
源文件
路径:wp-includes/comment.php
<?php
...
function _close_comments_for_old_posts( $posts, $query ) {
if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) )
return $posts;
/**
* Filters the list of post types to automatically close comments for.
*
* @since 3.2.0
*
* @param array $post_types An array of registered post types. Default array with 'post'.
*/
$post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
if ( ! in_array( $posts[0]->post_type, $post_types ) )
return $posts;
$days_old = (int) get_option( 'close_comments_days_old' );
if ( ! $days_old )
return $posts;
if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) {
$posts[0]->comment_status = 'closed';
$posts[0]->ping_status = 'closed';
}
return $posts;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/_close_comments_for_old_posts/