Skip to content

Instantly share code, notes, and snippets.

@hieuhuynh93
Last active June 13, 2022 07:18
Show Gist options
  • Save hieuhuynh93/51c31de2a05f9cf826a17372ff78ac59 to your computer and use it in GitHub Desktop.
Save hieuhuynh93/51c31de2a05f9cf826a17372ff78ac59 to your computer and use it in GitHub Desktop.
Enable comment
function enableCommentByCat() {
global $wpdb;
$getPosts = new WP_Query( array( 'category_name' => 'ten_category') );
if ( $getPosts->have_posts() ) {
while ( $getPosts->have_posts() ) {
$getPosts->the_post();
$postId = get_the_ID();
$wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET comment_status = 'open' WHERE ID=$postId"));
}
} else {
$wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET comment_status = 'closed' WHERE post_type='post'"));
}
}
enableCommentByCat();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment