2

I am trying to add defualt comments to my custom post type.

I have added to my supports argument:

'supports' => array('title','editor','thumbnail','tags','post-formats','comments','revision'),

I've also tried a few functions like this:

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' );

But nothing seems to work.Here is my custom post type:

function spark_register() {
$labels = array(
    'name' => _x('Spark Garden', 'post type general name'),
    'singular_name' => _x('Spark', 'post type singular name'),
    'add_new' => _x('Add New Spark', 'sparknz'),
    'add_new_item' => __('Add New Spark'),
    'edit_item' => __('Edit Spark'),
    'new_item' => __('New Spark'),
    'view_item' => __('View Spark'),
    'search_items' => __('Search Sparks'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => ''
  );
 $args = array(
    'labels' => $labels,
    'public' => true,
    'has_archive' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => 8,
    'supports' => array('title','editor','thumbnail','tags','post-  formats','comments','revision'),
          'taxonomies' => array('post_tag'),
); 
register_post_type( 'spark' , $args );
}


add_action('init', 'spark_register');

I've tried adding to 'twentysixteen' and activating but that still doesn't show the comments by default. I have it checked to allow comments in my settings, it does appear in my regular 'post' post type, but in my cutom post type the discussion checkbox is not checked and comments are not showing by default?

Thanks in advance..

4

I figured this out after some searching. For anyone who wants to know:

add_filter( 'comments_open', 'my_comments_open', 10, 2 );

function my_comments_open( $open, $post_id ) {

$post = get_post( $post_id );

if ( 'spark' == $post->post_type )
    $open = true;

return $open;
}
1
  • The documentation for comments_open mentions this caveat: "assuming your theme uses comments_open() to check if the comments are open."
    – pwbred
    Mar 7 '18 at 16:52

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.