0

I want to allow comments only for custom post types but not for normal posts or pages. What would be the best way to do that? I can think of one way that is a bit ugly.

One would be to remove the comment section from the post/page template in my child theme and then have the option under "Discussion" active.

I could hide the comments one posts and pages through CSS, but that wouldn't necessary prevent comments from being posted (even though they would be hidden on the frontend).

Is there a way to just remove the comment support for posts but keep it for custom post types? I also tried it like mentioned here: Is there ANY way to remove comments function and section totally?

But it doesn't hide the comment section underneath the post, if it's already active. Also wouldn't any new post have the comment section activated, if I have the option under "Discussion" active?

2 Answers 2

1
/**
 * Allow comments only for a specific custom post type.
 *
 * @param bool $open    Whether comments are open.
 * @param int  $post_id The post ID.
 *
 * @return bool Whether comments are allowed for the post.
 */
function restrict_comments_by_post_type($open, $post_id) {
    // Check if the post type is your custom post type
    if (get_post_type($post_id) === 'your_custom_post_type') {
        // Allow comments
        return true;
    }

    // Disable comments for all other post types
    return false;
}
add_filter('comments_open', 'restrict_comments_by_post_type', 10, 2);
add_filter('pings_open', 'restrict_comments_by_post_type', 10, 2);

The function takes two parameters: $open (whether comments are open) and $post_id (the ID of the post being checked).

use get_post_type($post_id) to retrieve the post type of the given post ID.

compare the post type with 'your_custom_post_type', which should be replaced with the actual slug or name of your custom post type. This condition checks if the current post type matches your custom post type.

If the post type matches, return true to allow comments for that post.

If the post type doesn't match, return false to disable comments for all other post types.

filters: comments_open and pings_open. Both filters use the same function restrict_comments_by_post_type, which means the restriction applies to both comments and pingbacks/trackbacks.

replace 'your_custom_post_type' with the actual name or slug of your custom post type.

1
  • Worked perfectly for me. Thanks, Pete. Sep 8, 2023 at 9:56
0

if you are using the code from the thread you linked to, to disable the comments on post and pages then when a new post/page is added it should have the comments automatically disabled.

The existing posts/pages will have comments enabled unless you uncheck the "Allow comments" checkbox when editing a post.

A quick and dirty way to disable comments for all posts is to run the below MySQL query from phpMyAdmin

UPDATE wp_posts SET comment_status = 'closed' WHERE post_type = "post"

You can also go to wp-admin / Posts panel, check all the posts, in "Bulk Action" select "Edit" and press "Apply", additional options will show, now in the "Comments" field select "Do not allow" and click "Update" button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.