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.
September 23, 2024
How does this handle posts after they're already published? Does it still keep comments open?
September 27, 2024
Hey Neil!
You'll see that posts usually keep comments open even after they're published! This is pretty helpful for regular discussions.
If you're changing options later it's always easy to manage. I usually use plugins to control it.
Feel free to ask more!
October 24, 2024
Do you think this strategy could maybe work for the other types of posts that I've set up? It's definitely been an issue
November 11, 2024
Hey Wendolyn!
Yes it can definitely work for the other types of posts you have set up. I've seen a lot of clients do well with similar methods. You might need to change it a little for each type because they can be different.
Please feel free to share more about your setup - I'd be really happy to help you out more! 😊