我一直在到处搜索一个php函数,以设置一个给定帖子的评论
仅找到用于检查注释状态的函数
<?php comments_open( $post_id ); ?>
我需要一个来设置评论<?php set_comments_open( $post_id ); ?>
但是没有人知道这个函数是什么,或者如果没有,怎么做?我一直在到处搜索一个php函数,以设置一个给定帖子的评论
仅找到用于检查注释状态的函数
<?php comments_open( $post_id ); ?>
我需要一个来设置评论<?php set_comments_open( $post_id ); ?>
但是没有人知道这个函数是什么,或者如果没有,怎么做?你试过去吗Posts 并选中标题旁边和下拉列表中的框"Bulk Actions" 选择Edit 然后apply, 下拉列表中的注释和"Allow".
并且在标题中的选项卡上的后期编辑区域中添加了一个名为"Screen Options" 并检查了名为“讨论”的字段?
并且在Settings->Discussion 是否已启用"Allow people to post comments on new articles"?
Add comments open to all posts
由调用的筛选器挂钩wp_insert_post
在插入或更新数据库和更新帖子之前comment_status
打开=真function comments_on( $data ) {
if( $data[\'post_type\'] == \'post\' ) {
$data[\'comment_status\'] = 1;
}
return $data;
}
add_filter( \'wp_insert_post_data\', \'comments_on\' );
今天遇到了这个。我认为这里公认的答案实际上并没有达到OP的要求。他在设置帖子以接受管理面板中的评论方面没有问题。他要求提供一个PHP函数,可以设置帖子的“comment\\u status”列。
我有一个相似的理由去寻找:我正在根据导入更新内容,所以这里的答案没有帮助。评论中的答案提供了在插入帖子时执行此操作的方法,但这不是一种很好的更新方法。我没有看到一个WP函数可以做到这一点-必须编写一个:
function set_post_comment_status( $post_id, $status = \'open\' ){
global $wpdb;
$wpdb->update( $wpdb->prefix . \'posts\', [ \'comment_status\' => $status ], [ \'ID\' => $post_id ] );
}
似乎是应该存在的东西。希望这对某人有所帮助。如果需要允许对已禁用的帖子发表评论,则需要更改的列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
}
您可以像这样添加代码$post->ID
是您已经拥有的帖子id
$my_post = array(
\'ID\' => $post->ID,
\'comment_status\' => \'open\'
);
wp_update_post( $my_post );
我正在尝试Q&A lite plugin from WPMU. 除了一件事,它看起来很棒。看起来用户只能问和答问题,不能对他们发表评论。这有效地缩短了两个用户之间的对话。一个问题,一个答案,就是这样,当然除了其他用户的答案,但不可能有这样的对话,我对它的有效性感到困惑。有人能告诉我我是不是用错了吗?我想不会,但如果我是,我想知道,否则谁能推荐类似的东西。