Code Snippet to Disable WordPress Comments on Specific Post Types

Marek S. Jan 6, 2025 Comments Management
How can I turn off comments for certain kinds of posts on my WordPress site?
What is the effective code snippet to disable comments for specific custom post types in WordPress?
Andy answered Jan 6, 2025

Disable Comments for Specific Post Types

Custom Code Solution

Add this code to your theme's functions.php file or in a site-specific plugin:

This code removes comment support and hides comment UI elements for specified post types:

function disable_comments_for_post_types() {
    // Add your post types to this array
    $post_types = array('page', 'product', 'portfolio');
    
    // Remove comment support
    foreach ($post_types as $post_type) {
        remove_post_type_support($post_type, 'comments');
        remove_post_type_support($post_type, 'trackbacks');
    }
    
    // Hide existing comments
    if (is_post_type_archive($post_types)) {
        add_filter('comments_open', '__return_false');
        add_filter('pings_open', '__return_false');
    }
}
add_action('init', 'disable_comments_for_post_types');

To also remove comment-related UI elements from the admin area:

function remove_comment_support_admin() {
    // Add your post types to this array
    $post_types = array('page', 'product', 'portfolio');
    
    // Remove comment metaboxes
    foreach ($post_types as $post_type) {
        remove_meta_box('commentstatusdiv', $post_type, 'normal');
        remove_meta_box('commentsdiv', $post_type, 'normal');
    }
}
add_action('admin_init', 'remove_comment_support_admin');

Plugin Solutions

If you prefer using a plugin, here are reliable options:

  1. Disable Comments - Most popular solution
  2. Remove Comments Absolutely - Lightweight alternative

Notes

  • Replace the post types in the $post_types array with your desired post types
  • The code works for both built-in and custom post types
  • For custom post types, you can alternatively set 'supports' => array() without 'comments' when registering the post type