WordPress: Disable comments on old posts

Depending on the topic of you posts, old posts might not be so relevant anymore (e.g. if they are news related). In this case comments added to old posts are most probably spam. So it makes sense to disable comments and ping backs after a certain number of days (e.g. 365 days).

Enabling or disabling comments is done in the database with the comment_status column of wp_posts e.g.:

UPDATE wp_posts SET comment_status = 'closed';

This will disable comments for all comments in the database. Of course if it is what you want to do, you can also from the beginning disable comments for all new posts. But this will not affect existing posts. For existing posts, you need the SQL statement above or you have to select all posts and edit the comment field in the administation UI.

Note that trackbacks and pingbacks are not affected by this. For them you have to update the column ping_status instead of comment_status.

If you want to only enable comments for registered users, the SQL statement would look like this:

UPDATE wp_posts SET comment_status = 'registered_only';

Now, if you do not want to disable comments for all posts but only for old posts, you have two possibilities:

  1. Create a cron job updating the comment_status column of old posts e.g.:
    UPDATE wp_posts SET comment_status = 'closed', ping_status = 'closed' WHERE post_status = 'publish' AND post_date < '2012-02-16';
  2. Introduce a filter which will based on the date make WordPress think comments where disabled in the database

The latter is the option we'll explore here.

We'll have to define a function doing the magic and add it as a filter to the_posts. Like this our function will be called everytime the posts are loaded. Our function will be called with an array of posts. We are only interested in the single post view. So we will basically make sure that we're there and update the comment_status and ping_status attributes of the first post. Here the code:

function close_old_comments( $posts ) {
	//Number of days after which comments should be closed
	$no_days = 365;
	//Only do this for pages where a single post is displayed
	if ( !is_single() ) { return $posts; }
	//Post age in seconds
	$post_age = time()-strtotime( $posts[0]->post_date_gmt);
	//Close comments for posts older than a year i.e. 365 days
	if ($post_age > ($no_days*24*60*60)) {
		//Disable comments
		$posts[0]->comment_status = 'closed';
		//Disable pingbacks and trackbacks
		$posts[0]->ping_status = 'closed';
	}
	//Return the updated post lists
	return $posts;
}
//Register our function as a filter on the_posts
add_filter( 'the_posts', 'close_old_comments' );

Add the code above to functions.php, save and go to a post older than a year. The new comment form will be gone.

2 thoughts on “WordPress: Disable comments on old posts

  1. I always allow comments because I think there’s always something new… even if it’s someone saying “This post isn’t relevant anymore, do this instead”… Sometimes an old bug fix post (for example) might have a comment saying “This is now fixed in a new service pack, etc…

    1. I guess it all depends on what kind of webpage or blog you have. If it’s news related it probabla doesn’t make much sense to allow comments after a few months or a year. If your posts usually have a longer validity, then it makes sense to keep the comments open. But even then, if the number of spam comments you get is high and the chances that you get a real useful comment is low, then it’s better to close the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *