• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Nile Flores Media

Helping You Rock Out Your Website Like A Rockstar

  • Home
  • About
  • Blog
  • Portfolio
  • Hire Me
  • Resources
  • Contact
  • Blogging
  • WordPress
  • Social Media
  • SEO
  • Web Design
  • News
  • Podcast
You are here: Home » WordPress » Disable Comments For Older Posts In WordPress

Disable Comments For Older Posts In WordPress

By Nile Flores Leave a Comment


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' );
?>


Filed Under: WordPress Tagged With: comments

About Nile Flores

Nile is a 43-year old female from the greater St. Louis (Southern Illinois side) area. Nile is a mother of 1 son. She is also a web designer and developer, a graphic designer, and a public speaker, who exclusively designs and develops using WordPress. She also blogs at GoDaddy's Blog, Verpex Hosting's blog and her very personal sites, Pixelled and Nail Polish Happy.




Related posts:

No related posts.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

Follow Me On:

  • Facebook
  • Instagram
  • LinkedIn
  • Twitter
  • YouTube

Get The Latest WordPress & Blogging Tips

Sign up for my newsletter, and also get my free ebook on 10 Reasons Why Your Website Isn't Converting, and How to Fix it!




Try to be More Positive on Social Media

Note: You can click on image and view the video in a larger lightbox window.

Footer

The Blog

Lots of free information, tutorials, and more to help you bring your best foot forward with your website.

  • Blogging
  • WordPress
  • Social Media
  • SEO
  • Web Design

Get The Latest WordPress & Blogging Tips

Sign up for my newsletter, and also get my free ebook on 10 Reasons Why Your Website Isn't Converting, and How to Fix it!

Copyright © 2025 · · WordPress