1

I've created a new custom post type for a specific posts. The posts from my new custom post type, have - by default - set the comments to "off". I need to the comments to be "on" by default.

In my functions.php file I have this:

'supports' => array('editor','comments')

and

function default_comments_on( $data ) {
    if( $data['post_type'] == 'registro' && $data['post_status'] == 'auto-draft' ) {
        $data['comment_status'] = 1;
    } 

    return $data;
}
add_filter( 'wp_insert_post_data', 'default_comments_on' );

But it didn't mark the box to comment by default. Any tips?

My default posts, don't show the metabox for comments, and the comments are allowed by default. I want to do exactly this with my new custom post type. I mean: Don't show the metabox and turn Comments on by default.

The register:

function create_post_type_registro() {

/**
 * Labels customizados para o tipo de post
 * 
 */
$labels = array(
    'name' => _x('Registros', 'post type general name'),
    'singular_name' => _x('Registro', 'post type singular name'),
    'add_new' => _x('Adicionar novo', 'film'),
    'add_new_item' => __('Adicionar novo registro'),
    'edit_item' => __('Editar registro'),
    'new_item' => __('Novo registro'),
    'all_items' => __('Todos os registros'),
    'view_item' => __('Ver registro'),
    'search_items' => __('Procurar registros'),
    'not_found' =>  __('Nenhum registro encontrado'),
    'not_found_in_trash' => __('Nenhum registro encontrado na lixeira'),
    'parent_item_colon' => '',
    'menu_name' => 'Registros'
);

/**
 * Registamos o tipo de post registro através desta função
 * passando-lhe os labels e parâmetros de controle.
 */
register_post_type( 'registro', array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,   
    'show_ui' => true,
    'show_in_menu' => true,
    'has_archive' => 'registros',
    'rewrite' => array(
     'slug' => 'registros',
     'with_front' => false,
    ),
    'capability_type' => 'post',
    'has_archive' => true,
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('editor','comments')
    )
);
1

Custom post types support comments by default. I'm not sure how you registered your post type since you didn't include the full source but the codex has good examples on how to do that.

If the metabox is showing in your custom post type and you are trying to hide it you could click on Screen Options in the top right of your browser and uncheck comments discussion. This just hides the comment metabox in the edit screen.

enter image description here

Make sure you are including the comment template part in your single.php or page.php.

====== Edit 2 - Correct answer ======

After looking into this further it looks like something is overriding the comment status. Putting the below functions will do what you want

// Sets the comments to allowed by default
function turn_on_comments() { 
   update_option('default_comment_status', 'open');
} 
add_action('update_option', 'turn_on_comments');

// Hides the metabox in the edit screen (replace post-type-here with your custom post type)
function remove_meta_boxes() {
    remove_meta_box('commentstatusdiv', 'post-type-here', 'normal');
}
add_action('admin_menu', 'remove_meta_boxes');
  • As i said i'm new in wordpress, so i dont know how to explain to you how i registered it. But,i'll try to explain it better. Categories metabox, i can show and hide , removing it from supports array ... thats not my problem. The problem is: By default for my custom post type, the select box to allow comments are not selected, so the poster need to click to allow comments. I need it to be selected as default, allowing comments without select it. My posts (the default post type), works this way, and plus it doesnt shows the metabox. I want it for my custom post type. Sorry, my english sucks – Leibovich Jul 24 '13 at 23:43
  • I put the register code on the Answer - @kel – Leibovich Jul 24 '13 at 23:46
  • @Leibovich Under Settings > Discussions is "Allow people to post comments on new articles" checked? Make sure it is, this is what makes the Allow comments option to be checked by default. – kel Jul 25 '13 at 2:30
  • Yes.. its checked... i already unchecked , save, checked again and save again,... =/ BTW ... i'm using 3.1.2 – Leibovich Jul 25 '13 at 2:31
  • There might be something in a plugin or theme that is overriding that. If you look at my Edit 2 I added the option to hopefully force it checked. – kel Jul 25 '13 at 6:33
0

The previous answer is not working for me and I think it can not work for most people. Even if it works for someone, it overrides the WordPress configuration site-wide and it is not specific for the required custom post type.

For me, the correct way to enable comments in a custom post type and remove the comments status meta box is something like this (assuming post type is registered with support for comments):

add_action('admin_menu', 'cyb_remove_meta_boxes');
function cyb_remove_meta_boxes() {

    remove_meta_box('commentstatusdiv', 'post-type-here', 'normal');

}

add_filter( 'wp_insert_post_data', 'cyb_comments_on' );
function cyb_comments_on( $data ) {

    if( $data['post_type'] == 'post-type-here' ) {

        $data['comment_status'] = "open";

    }

    return $data;

}

Or, maybe better, if we want to allow editors to close comments:

add_action('admin_menu', 'cyb_remove_meta_boxes');
function cyb_remove_meta_boxes() {

    if( ! current_user_can( 'edit_others_posts' ) ) {

        remove_meta_box('commentstatusdiv', 'post-type-here', 'normal');
        add_filter( 'wp_insert_post_data', 'cyb_comments_on' );

    }


}

function cyb_comments_on( $data ) {

    if( $data['post_type'] == 'post-type-here' ) {

        $data['comment_status'] = "open";

    }

    return $data;

}

Also, I think that Codex is wrong as it says that comment_status is set to default comments status. It means that, if the custom post type supports comments and the default comment satus in WordPress configuration is open for new posts, any new post should have comments open by default but that is not happening if you remove the comment status meta box. That is why it is needed to override comment_status when the post is inserted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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