如果需要允许对已禁用的帖子发表评论,则需要更改的列comment_status
该职位的open
.
正如JBoss所示,您可以通过wpdb语句来实现这一点,但是您也可以只使用本机WordPress post更新功能: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);
}
如果需要循环浏览所有帖子来完成此操作,可以使用
WP_Query
要这样做:
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
}