我想私下发表意见。我的意思是评论必须对帖子作者和评论作者可见。我创建了自定义注释部分“Better-Comm.php”。我用过pre_get_comments,但我做不到工作。请帮帮忙
< id="comment-">
comment_type ) :
case 'pingback' :
case 'trackback' : ?>
%s says:', 'textdomain' ), get_comment_author_link() ); ?>
comment_approved == '0' ) { ?>
$add_below,
'depth' => $depth,
'max_depth' => $args['max_depth']
) ) ); ?>发布于 2022-02-04 19:29:50
您可以使用pre_get_comments筛选器在获取注释之前修改注释查询的参数。特别是author_in参数。
我试着编写一个示例,尽管我还没有测试它,但是它类似于这样:
add_action( 'pre_get_comments', 'author_and_self_comment_filter' );
function author_and_self_filter( \WP_Comment_Query $query ) : void {
// We need to do some checks first and return early if
// this filter doesn't apply, e.g. if you're the post
// author etc
// don't break the admin UI
if ( is_admin() ) {
return;
}
// only filter if we're grabbing the comments for a post
if ( $query->query_vars['post_id'] === 0 ) {
return;
}
// only logged in users should see comments
if ( ! is_user_logged_in() ) {
// we need to return 0 results so I'm asking for a
// comment type that doesn't exist so there are no
//results, there's probably a better way to do this:
$query->query_vars['type'] = 'banana';
return;
}
// this is already filtered to specific people! Skip!
if ( ! empty( $query->query_vars['author__in'] ) ) {
return;
}
// Admins + super admins see everything!
if ( current_user_can( 'manage_options' ) || is_super_admin() ) {
return;
}
// get my ID
$my_user_id = get_current_user_id();
// get the post author ID
$post_id = $query->query_vars['post_id'];
$p = get_post( $post_id );
$post_author_id = $p->post_author;
// If I'm the post author then I can see everything
if ( $my_user_id === $post_author_id ) {
return;
}
// now we need to set the `author_in` to an array with 2
// values, the authors user ID and the current users ID
$authors = [];
$authors[] = $my_user_id; // My user ID
$authors[] = $post_author_id; // Author ID
$query->query_vars['author_in'] = $authors;
}我们需要进行一系列检查,以确定我们是否需要这样做,最后,我们只对作者和当前用户进行评论。
请注意,如果您这样做,评论计数器将永远不会准确,而且可能非常不可靠,如果不取消限制,就无法修复此问题,因此所有的注释对所有用户都是可见的。
https://wordpress.stackexchange.com/questions/402237
复制相似问题