Lately I’ve been publishing a lot of design-related articles and haven’t paid much attention to WordPress topics. Today I’m sharing a small snippet that most blogs may not need, but could be useful for resource-constrained blogs or specialized WordPress sites. When a post’s comment count reaches a certain threshold, comments are automatically disabled. (This is primarily intended for specialized WordPress sites, such as news commentary sites.)
Add the following code to your theme’s functions.php file:
// Auto-close comments after a certain count
function disable_comments($posts) {
if (!is_single()) {
return $posts;
}
if ($posts[0] - >comment_count > 100) {
$posts[0] - >comment_status = 'disabled';
$posts[0] - >ping_status = 'disabled';
}
return $posts;
}
add_filter('the_posts', 'disable_comments');
The code above automatically closes comments when the count reaches 100. Adjust the value on line 6 to suit your needs.