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.

1 Views

Tags

Language

Did it work?

Save Snippet

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.