Automatically shut off comments for stale blog entries in WordPress Hack

November 18th, 2003
WordPress Hack

I got a request in the WordPress forums to have a way of autmatically turning off comments for entries that fall off the first page and here are the results. My idea is turn off comments for the posts that fall off your list once you post. Quite simple actually. It takes the post_ID of your new post (which is published status) , subtracts a pre-determined value to get the post for which comments need to be shut off, and then sets that information in the database.
Now that I try to post the hack, I realize it might be really difficult to find for non-programmers. Click here to get the hacked b2edit.php and save it under your wp-admin directory.

For WordPress 1.0+ :
Find post.php in your wp-admin folder and find this line:
} // end if publish
(this shows up twice, you want the one that is after the case ‘post': )

and add this right above that // end if publish line:

//Comment off hack by LaughingLizard
$posts_per_page = get_settings(‘posts_per_page’);
$post_comment_fix_ID = $post_ID – $posts_per_page – 2;
$check_vars = $wpdb->get_results(”
SELECT post_title, ID FROM $tableposts
WHERE ID < = $post_comment_fix_ID AND comment_status = 'open' "); if ($check_vars) { foreach ($check_vars as $check_var) { if (!preg_match ("//i”, $check_var->post_content)) {
$comment_close = $check_var->ID;
$result = $wpdb->query(”
UPDATE $tableposts SET
comment_status = ‘closed’
WHERE ID = ‘$comment_close’ “);
//End of Comments off hack

That’s it.




  1. Brian Poirier (1 comments.) says:

    I installed it as above, but it does not seem to work for me.
    As an example, I just put a comment here: which is a post much older than anything on the front page.

  2. vika (2 comments.) says:

    I pasted the code in the right place, and when going to

    I get the following error:

    Parse error: parse error, unexpected T_STRING in /var/www/wordsend/wp-admin/post.php on line 409

    Line 409 is this one:

    SELECT post_title, ID FROM $tableposts


  3. Mark says:

    This is NOT valid for anything above WordPress 1.0

  4. vika (2 comments.) says:

    Oh! Sad. I did look at your post carefully, and it says 1.0+. Okay, then.

  5. alex says:

    Did you think about doing it by date instead of post ID? Post ID can’t be a bit unpredictable when you take into account draft posts and private posts.

  6. Mark (36 comments.) says:

    Actually, my first instinct was to use the postID because it was readily available. But now that I have had some time to think about it and I have had to fix a few bugs that I did not think about, I believe that using the postID poses some issues. How about searching for entries where post_status=”publish” before including them in the count? I might incorporate that when I get a chance.

  7. Sara (3 comments.) says:

    Could this please be updated for WP 1.0 and higher? I left a message on the WP forum
    board about this. :) Thanks!

  8. Mark says:

    Done. :) Look again.

  9. Mark (36 comments.) says:

    Thats a hard error to fix! :mrgreen:
    Give us some more information please.


  1. SPAM Prevention with WP!!
    Are you being spammed? Do you use WP? If the answer is YES, then you MUST read this…..

Obviously Powered by WordPress. © 2003-2013

page counter