Written by Sean Behan on Sun Jun 17th 2012

To turn comments off programmatically with a filter in Wordpress, you can set the post object's comment_status variable to "closed". Call the filter at some point after the post is loaded but before the comments are rendered. This is a hack, but I haven't seen a simpler approach, beside installing another plugin. In the example below, the condition to disable comments is simple. If a post is in a certain category, comments aren't allowed. Otherwise, go ahead and behave as normal. You can still disable comments on a per post basis in other categories.

One thing to note is the use of the global $post object. The function needs access to the object to set the variable.

add_filter('get_header', 'sb_turn_comments_off');
function sb_turn_comments_off(){
    if(in_category("Projects") AND is_single() ){
      global $post;
      $post->comment_status="closed";
  }
}
I've only tested this with the Thematic framework. I assume that other themes will check the comment_status variable before allowing comments.

Tagged with..
#comments #content #global #plugin #post #programmatically #thematic #Wordpress #Programming

Just finishing up brewing up some fresh ground comments...