Paulund
2014-05-19 #wordpress

Remove Comments From Certain Post Types

In WordPress the default posts and pages both come with comments enabled, this will allow WordPress to display your comment form under your content. But with custom post types you can choose if this content supports comments or trackbacks. When you create a custom post type you will use the function register_post_type().


<?php register_post_type( $post_type, $args ) ?>

One of the arguments parameters is for what the post type supports, these will provide the different options for the post type from. - title

  • editor (content area)
  • author
  • thumbnail (featured image, current theme must also support post-thumbnails)
  • excerpt
  • trackbacks
  • custom-fields
  • comments
  • revisions
  • page-attributes
  • post-formats

As you can see you can pass the parameters of comments and trackbacks if you want the post type to support this. But what if you have a post type that is added by a plugin, then your code isn't the one that is creating the post types. There is a built in function in WordPress to remove what the post type support.

<?php remove_post_type_support( $post_type, $supports ) ?>

This takes two parameters, first is the post type you want to change and second is the feature you don't want the post type to support. This function needs to run on the init action of WordPress, so you can use the following code to remove comments from any post types you want.


function theme_init( ) {
    remove_post_type_support('custom-post-type', 'comments');
    remove_post_type_support('custom-post-type', 'trackbacks');
}
add_filter( 'init', 'theme_init' );