Skip to main content
added 90 characters in body
Source Link
Frits
  • 190
  • 4
  • 13

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
}

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'
    );

    //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
}

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
}
Source Link
Frits
  • 190
  • 4
  • 13

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'
    );

    //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
}