_close_comments_for_old_posts()
Closes comments on old posts on the fly, without any extra DB queries. Hooked to the_posts.
Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.
Hooks from the function
Returns
WP_Post[].
Usage
_close_comments_for_old_posts( $posts, $query );
- $posts(WP_Post[]) (required)
- Array of post objects.
- $query(WP_Query) (required)
- Query object.
Changelog
| Since 2.7.0 | Introduced. |
_close_comments_for_old_posts() close comments for old posts code WP 7.0
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 string[] $post_types An array of post type names.
*/
$post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
if ( ! in_array( $posts[0]->post_type, $post_types, true ) ) {
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;
}