Set post comments open function

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

Read More
<?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?

Related posts

Leave a Reply

4 comments

  1. 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' );
    
  2. 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.

  3. You can add code like this $post->ID is post id which you already have

    $my_post = array(
        'ID'            => $post->ID,
       'comment_status' => 'open'
    );
    
    wp_update_post( $my_post );
    
  4. 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
        
        //arguments
        $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
        wp_insert_post($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
    }