It is really annoying to get spam in posts so old you almost forgot about them. Here is a tutorial to help you with close your comments using the close comments function in version WordPress older than 2.7. This will help bring down the number of spammers who like to slip in comments on older posts.
In WordPress 2.7 and above, You can change this by going to your Admin Panel under Settings> Discussion. There is a section on “Other Comment settings”. You will be able to set how many day you want your posts open under “Automatically close comments on articles older than X days.”
Step 1. Open your close_comments statement with an if statement saying that you want to apply the function to your posts.
<?php
function close_comments( $posts ) {
if ( !is_single() ) { return $posts; }
Step 2. You want specify if the post is written a certain time, you want it to close in so many days
if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {
$posts[0]->comment_status = 'closed';
$posts[0]->ping_status = 'closed';
}
Step 3. You want to close the function, but this is not just a function, but a filter, so you must apply it as a filter to work.
return $posts;
}
add_filter( 'the_posts', 'close_comments' );
?>
Leave a Reply