Automatically Close Comments on Older Posts
To cut on down on SPAM comments, as well as to keep your content fresh, you can close comments on older posts automatically using SQL. This script is designed to run in the footer of your blog each time a page is loaded. To ensure it doesn’t load too often, we can wrap a simple check around the function which will ensure it runs only once per month:
php
add_filter( 'the_footer', 'old_comments' );
function old_comments( $post ) {
if ((get_option('monthly_maintenance') != date('n')) {
update_option('monthly_maintenance', date('n'));
global $wpdb;
if (date('n') == 1) {$y=date("Y")-1; $m=12} else {$y=date("Y"); $m=date('n')-1;}
$d = cal_days_in_month(CAL_GREGORIAN, $m, $y);
$wpdb->query("UPDATE `wp_posts` SET `comment_status` = 'closed', `ping_status` = 'closed' WHERE `post_date_gmt` < '$y-$m-$d 23:59:59'");
}
}
?>

Leave a Comment