WordPress tutorials: Disable comments on old posts

Wpsnipp – Sometimes, it can be useful to automatically disable commenting on posts older than X days. There’s no built-in function in WordPress to do that, but if you still like to do it, just read this recipe.Adding this PHP code to your functions.php will disable the comments on posts that are older then one month.

 

 

<?php
function close_comments( $posts ) {
if ( !is_single() ) { return $posts; }
if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {
$posts[0]->comment_status = 'closed';
$posts[0]->ping_status    = 'closed';
}
return $posts;
}
add_filter( 'the_posts', 'close_comments' );
?>

Be Sociable, Share!

Leave a comment