I've been searching high and low for a php function to set a comments of a given postID open

only found function to check what the comment status is

<?php comments_open( $post_id ); ?>

I need one to set comment would expect it to be

<?php set_comments_open( $post_id ); ?>

But it isnt anybody got any idea what the function is or if there isnt one how to do it?

  • Do you want to check if comments are enabled on a specific post or do you want to control witch post you can comment on? – Pontus Abrahamsson Jul 19 '12 at 19:33
  • I want to enable post to be able to have comments, so the comment_status has to be set to 'open' instead of 'closed' – Bram Jul 19 '12 at 19:55

Have you tried go to Posts and checked the box next to the title and in the dropdown "Bulk Actions" choose Edit and then apply, Comments and in the dropdown "Allow".

And also have added the screen in the post edit area on the tab in the header called "Screen Options" and checked the field called: Discussion?

And in Settings->Discussion have you enabled "Allow people to post comments on new articles"?

Add comments open to all posts

A filter hook called by the wp_insert_post function prior to inserting into or updating the database and update the post comment_status to open = true

function comments_on( $data ) {
    if( $data['post_type'] == 'post' ) {
        $data['comment_status'] = 1;

    return $data;
add_filter( 'wp_insert_post_data', 'comments_on' );
  • yep but I'm looking for a php way because I'm importing products thru an addapted datafeed script. So in php while importing i have to set comment_status, when the script imports feeds its on closed so have to find way in php to set it open. The allow people to post comments on new articles, is on just checked. – Bram Jul 19 '12 at 20:43
  • I had to set comment_status to a string 'open' or 'closed' for this to work as expected. See developer.wordpress.org/reference/functions/wp_insert_post – Michael May 24 '19 at 8:44

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.