WordPress Tip: Disable Comments in Old Posts via PHP
Post #578 categorized as WordPress, last updated on acid
Tagged with comments, php, pingbacks, posts, sql, tips, trackbacks, tricks, WordPress
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.
Share this..
Related articles
- WordPress Discussion Management: Enable or Disable Comments and Pingbacks via SQL
- Display the Total Number of WordPress Posts, Comments, and Categories
- Fruit Loop: Separate any Number of Odd and Even Posts from any Category in WordPress
- Horizontally Sequenced Display Order for WordPress Posts in Two Columns
- WordPress Basics: Saving and Editing Posts
- WordPress Basics: Publishing Posts
- The Deluxe One-Minute Dofollow WordPress Upgrade
#1 — Nikhil G.
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.