Site logo

Archived topic

Disabling comments on custom post-type

7 replies · Started by Juste on February 28, 2022

Viewing posts 1–8 of 8

Hello.
I'm trying to disable comments on custom post type with the following snippet but no luck.

add_action('wp', 'jm_simple_comments', 15);

function jm_simple_comments() {
  if( is_singular('mec-events') ) {
		remove_action( 'generate_after_do_template_part', 'generate_do_comments_template', 15);	
}

Please help

Hi there,

can you share a link to where i can see the CPT with Comments ?

here you go

howdy. I was mislead by the fact that filtering worked.
Thanks.
You are a saviour.

Glad to be of help

Acutally, in case anyone lands on this page, I found a universal useful bit here.

function my_prefix_comments_open( $open, $post_id ) {
    $post_type = get_post_type( $post_id );
    // allow comments for built-in "post" post type
    if ( $post_type == 'post' ) {
        return true;
    }
    // disable comments for any other post types
    return false;
}
add_filter( 'comments_open', 'my_prefix_comments_open', 10 , 2 );

The conditional check can even be outside of the handle and we just use add_filter( 'comments_open', function(){return false;} );
Graceful.

Thanks for sharing that :)

This archived topic is closed to new replies.