Why are the comments disabled by default on my custom_post_types?

I am using a custom post type + taxonomies in a podcast solution for a client. The setting (settings >> discussion) for comments is set to “allow comments”. When I add / edit posts under the “posts” tab, comments are enabled by default.

However, when I add / edit posts under the custom post type (podcast) – comments are disabled by default.

The user can still manually enable comments for each post, but this is obviously not ideal. Thoughts?

UPDATE: Relevant Code in functions.php

function create_my_post_types() {
    register_post_type( 'podcast',
        array(
            'labels' => array(
                'name' => __( 'Podcast' ),
                'singular_name' => __( 'Podcast' ),
                'new_item' => __( 'New Episode' ),
                'add_new_item' => __( 'Add New Episode' )
            ),
            'public' => true,
            'hierarchical' => true,
            'menu_icon' => get_stylesheet_directory_uri() . '/assets/podcast-icon.png', // 16px16
            'menu_position' => 9,
            'supports' => array( 'title', 'editor', 'comments', 'post-templates'),
            'register_meta_box_cb' => 'add_podcast_metaboxes' // This registers the metabox that we'll add later.
        )
    );
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Ok – so I solved this. Here is what appears to be the problem.

Comments are disabled by default for custom-post-types. This happens even if
you have them enabled in the overall settings

To fix it, all I had to do was the following:

  1. In SETTINGS > DISCUSSION uncheck the “Allow people to post comments on new articles” setting.
  2. Click “Save Changes”
  3. Now go back and re-check “Allow people to post comments on new articles”
  4. Click “Save Changes”

It seems that for custom post types you need to kind of kick-start this setting. All new posts for created custom-post-types will have the box enabled by default. Existing posts will retain their previous setting. I’m assuming it’s the same for allowing trackbacks. Hopefully this helps someone…

Method 2

This is the default state.. you need to change the “Supports line” where you build your custom post type..

Meaning this line:

// or something similer that starts the same

'supports' => array('title','editor','author','excerpt','revisions'),

To this line:

'supports' => array('title','editor','author','excerpt','comments','revisions'),

Hope this helps.
Cheers, Sagive.

ADDED FUNCTION (INSIDE FUNCTIONS FILE):

function default_comments_on( $data ) {
    if( $data['post_type'] == 'your_custom_post_name' ) {
        $data['comment_status'] = 1;
    }

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

Method 3

None of the following suggestions worked for me. Sagive SEO’s function almost worked for me. I checked my database to find that the only values for comment_status are open and closed. That function inserted 1 as a value. I revised the function and it seems to work perfect now. I hope this helps someone.

function default_comments_on( $data ) {
    if( $data['post_type'] == 'your_custom_post_type_name' ) {
        $data['comment_status'] = 'open';
    }

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

Method 4

Just taking a random stab but do you have to add “comments” to the supports array parameter, in the register_post_type() call?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x