Automatically Close Comments on Old Posts
This snippet automatically closes comments on posts older than a specified number of days.
PHP
function close_comments_on_old_posts($posts) {
if (!is_admin()) {
$days_old = 30; // Change this to your desired number of days
foreach ($posts as $post) {
if (strtotime($post->post_date) < strtotime("-$days_old days")) {
$post->comment_status = 'closed';
$post->ping_status = 'closed';
}
}
}
return $posts;
}
add_filter('the_posts', 'close_comments_on_old_posts');
Snippet Feedback
Did this snippet work for you? Do you have any questions about this snippet? Leave some feedback below.
SHARED BY
1 Views