Plugin Sale! Save 15% on pro plugins with discount code: FALL2020
Web Dev + WordPress + Security

WordPress Tip: Disable Comments in Old Posts via PHP

Just a quick WordPress snippet for future reference. I recently explained how to disable comments, pingbacks, and trackbacks via SQL. Here’s a good way to do it via PHP:

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

You can run this script as a plugin, through your theme’s functions.php, or through a custom user-functions.php file. Simply set the desired number of days by changing the number “30” to whatever you would like. As is, this script will close comments, pingbacks and trackbacks on all articles posted more than 30 days ago.

Jeff Starr
About the Author
Jeff Starr = Fullstack Developer. Book Author. Teacher. Human Being.
WP Themes In Depth: Deep dive into WP theme development.

8 responses to “WordPress Tip: Disable Comments in Old Posts via PHP”

  1. Nikhil G. 2008/07/08 5:22 pm

    Hi perishable..

    :D when i came at your site, for Apathy theme. It was quite absurd to see the blank page returned when you try to publish a comment.

    So, is there a way to use this script and if comments are closed for that particular post, then a “NO COMMENTS ON OLD POST” message or something like that is displayed. ??

    It become quite confusing else.

    Thanks,
    Nikhil G.

  2. Jeff Starr
    Jeff Starr 2008/08/04 8:15 am

    Hi Nikhil, somehow I missed this comment when it was first posted. I will look into the Apathy theme and see if I can figure out what’s happening with the comments.
    Regards,
    Jeff

  3. Jeff Starr
    Jeff Starr 2008/08/05 7:06 pm

    Nikhil, I just finished testing the Apathy theme. Everything seems to be working great. Commenting works perfectly on posts with open comments; posts with closed comments display a clear message indicating that comments are closed.

    Looking at the date that you left the comment on this post, I think my server may have been causing the problem. Every now and then a blank white page appears after posting a comment. This is an issue with my web host and in no way represents the functionality of any of my themes.

    I hope this helps resolve any confusion and/or frustration that you might have experienced while using the site. Please let me know if I may be of any additional assistance with this matter.

    Regards,
    Jeff

  4. Just today I decided I was sick of people commenting on really really old posts (like I’m doing right now). Well, not “sick”, but a lot of times it’s support requests for stuff I just can’t possibly keep up with, so I thought the best thing to do would be to just close up comments and people can directly contact me or use my forums if they need help.

    The above code, in a functions.php file as part of my theme, works absolutely perfectly.

  5. Jeff Starr
    Jeff Starr 2009/02/08 2:14 pm

    Hi Chris, I totally hear you. I had this script in place for awhile on a previous theme and it worked great. Lately I have been getting bombed again on old open posts. High time to drop this code into my current theme’s functions.php. Thanks for the reminder! :)

  6. This function works great, but it crashes my 404 error page.

    If this function is active and somebody makes a search with no results, 404 page should be shown, but instead of that I get a blank page with the ‘no comments allowed to this old post’ message.

    If I deactivate this function, everything works again. Im using wp 2.7.1.

  7. Jeff Starr
    Jeff Starr 2009/02/25 1:54 pm

    @DogDay: Newer versions of WordPress should use the built-in auto-disable feature tucked away in the “Discussion” area of the WP Admin. Otherwise, for older versions of WordPress, this method should work just fine.

  8. wp_developer 2011/06/18 7:33 am

    this function work fine but i have one issue I put the function in my theme function.php and I changed the time to 6 * 60 * 60 thats mean I want to disable the comment for post after 6 hours, my issue is when a user try to post a comment he will get a message tell him that the comments are close but when I display the posts with its all comments I see its been added there?

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
WP Themes In Depth: Build and sell awesome WordPress themes.
Thoughts
Past week here in WA state has been hellish. So much smoke, like living in a chimney.
Now in September, I’m where I wanted to be in March.
Spent some time updating my article on unsafe characters, once again current with latest IETF specification.
Just realized that “Neo” is an anagram for “One”. As in, “he is the One” (The Matrix).
To get VLC app to load all songs (including subfolders), go to Preferences ▸ Show All ▸ Playlist ▸ Subdirectory behavior ▸ Expand.
Switching from PhotoShop to Affinity Photo is one of the most liberating work-related things I've done in 20 years.
Finally got my collection of Pro WordPress plugins updated. Everything now better than ever and current with WP 5.5.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.