Open Comments on Custom Post Types in WordPress
I noticed some custom post types will have comments disabled by default in WordPress, despite them being enabled globally.
To fix this, you can add the following to your functions.php file in your theme:
/*
* Enable comments for "your_post_type_here" custom post type
*/
function enable_comments_on_code_post_type_save( $post_ID, $post, $update ) {
if ( 'your_post_type_here' === $post->post_type && !wp_is_post_autosave( $post_ID ) ) {
remove_action( 'save_post', 'enable_comments_on_code_post_type_save', 10, 3 );
wp_update_post( [
'ID' => $post_ID,
'comment_status' => 'open',
] );
add_action( 'save_post', 'enable_comments_on_code_post_type_save', 10, 3 );
}
}
add_action( 'save_post', 'enable_comments_on_code_post_type_save', 10, 3 );
Just remember to replace the two "your_post_type_here" instances above with the name of your post type.
How does this work?
Every time you save or publish a new post with your custom post type, this code checks to make sure that comments on that post are enabled.
Will it work retroactively on old posts?
No. You'll have to make a small modification to each posts. You can select multiple posts with the batch editor in the WordPress post list and make a change to all of them. For example, change the author from Author A to Author B, and then back to Author A. This will trigger an update on all of your posts and comments will be enabled on all of them. Moving forward, all new posts will have comments enabled automatically.
Comments