While working on a project recently I found that I wasn’t able to control whether comments were allowed or not for specific post types. The client asked us to disable comments by default for pages and a custom post type, but to enable comments by default for her blog posts. She asked us to make the comment option available for her custom type and pages, however, in the event that she changed her mind at a later date. I was surprised to see that this was not something that could be done right away via the WordPress back-end, I found that I could only globally allow or disallow commenting by default.

I searched for quite a while and I wound up putting together my own function with what I found. Here’s the function that you can put into your functions.php, replacing the post type names with your own post types:

function default_comments_off( $data, $postarr ) {
     if( $data['post_type'] == 'page' || $data['post_type'] == 'my-custom-post-type' ) {
          //New posts don't have an ID - So this checks if the post is new or already exists
          if( !($postarr['ID']) ){
               $data['comment_status'] = 0; //0 = false | 1 = true
          }
     }
     return $data;
}
add_filter( 'wp_insert_post_data', 'default_comments_off', '', 2);