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

Came upon this one today. I don't think the accepted answer here actually gets at what the OP is asking. He's not having trouble with setting up a post to accept comments in the admin panel. He's asking for a PHP function that can set a post's 'comment_status' column.

I had a similar reason for looking: I'm updating content based on an import, so the answer here doesn't help. The answer in the comments gives a way to do this when INSERTING a post, but not a good way to update. And I don't see a WP function that exists to do this - had to write one:

function set_post_comment_status( $post_id, $status = 'open' ){
    global $wpdb;
    $wpdb->update( $wpdb->prefix . 'posts', [ 'comment_status' => $status ], [ 'ID' => $post_id ] );

Seems like something that should exist. Hopefully that helps someone.


If you need to allow comments on a post that have already had them disabled, you will need to change the column for comment_status of that post to open.

As JBoss has shown, you can do this through a wpdb statement, however you can also just make use of the native WordPress post updating function: wp_insert_post

function reopen_comments_of_post($post_id) {
    if(comments_open($post_id)) {return;} //return if comments are already open
    $post = array(
        'ID'            => $post_id,
        'comment_status'=> 'open',
        'post_title'    => get_the_title($post_id), //content must be added as per specs
        'post_content'  => get_the_content($post_id), //title must be added as per specs

    //update post

If you need to loop through all of your posts to do this you can make use of a normal WP_Query to do it like this:

add_action('wp', 'loop_through_all_posts_and_open_comments');
function loop_through_all_posts_and_open_comments() {
    //arguments for posts
    $args = array(
        'post_type'     => 'my_custom_post_type', //can be post / product / etc
        'posts_per_page'=> -1, //all posts

    //create loop
    $query = new WP_Query($args);
    if($query->have_posts()) { //check query has returned posts
        while($query->have_posts()) { //loop through posts
            $query->the_post(); //load post
            reopen_comments_of_post($query->post->ID); //fix comment status
    wp_reset_postdata(); //clear post object data

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.