在评论页面中(/wp-admin/edit-comments.php
), 每个登录的用户都可以看到所有站点评论
我希望用户只看到他/她自己的评论以及他/她的帖子上留下的评论。
如何过滤此内容?
在评论页面中(/wp-admin/edit-comments.php
), 每个登录的用户都可以看到所有站点评论
我希望用户只看到他/她自己的评论以及他/她的帖子上留下的评论。
如何过滤此内容?
这三项中的任何一项都将帮助您:
//Before getting the comments, on the WP_Comment_Query object for each comment
add_action(\'pre_get_comments\', \'wpse56652_filt_comm\');
//Applied on the comments SQL Query, you can modify the \'Where\' part of the query
add_filter(\'comments_clauses\', \'wpse56652_filt_comm\');
//After the comments are fetched, you can modify the comments array
add_filter(\'the_comments\', \'wpse56652_filt_comm\');
function wpse56652_filt_comm($param) {
//access the current user
global $current_user;
get_currentuserinfo();
//current users id = $current_user->ID;
//Current users posts, check get_posts params to change as per your need
$user_posts = get_posts(array(\'author\' => $current_user->ID, \'posts_per_page\' => -1));
echo \'<pre>\';
print_r($param);
echo \'</pre>\';
return $param;
}
此外,您还可以使用global $pagenow
以确保代码仅在此页面上运行。对不起,我今天有点不舒服,所以无法写下一个例子!;)
编辑:
/**
* Show only the Comments MADE BY the current logged user
* and the Comments MADE TO his/hers posts.
* Runs only for the Author role.
*/
add_filter(\'the_comments\', \'wpse56652_filter_comments\');
function wpse56652_filter_comments($comments){
global $pagenow;
global $user_ID;
get_currentuserinfo();
if($pagenow == \'edit-comments.php\' && current_user_can(\'author\')){
foreach($comments as $i => $comment){
$the_post = get_post($comment->comment_post_ID);
if($comment->user_id != $user_ID && $the_post->post_author != $user_ID)
unset($comments[$i]);
}
}
return $comments;
}
有没有办法实现这些功能wp_count_posts, wp_count_terms 和wp_count_comments 仅为特定用户返回结果,可能使用用户的ID?我正在为自定义帖子类型制作一个自定义的“即时”仪表板小部件,我需要它只显示当前用户的数据,而不是整个站点的数据。提前谢谢。EDIT: 为了回应@kaiser在下面的评论,我做了以下操作,但什么都没有发生-结果与过滤器关闭时的结果相同(不应该是这样-检查此用户是否有不同数量的已发布employee 职位类型)。我的函数中的代码似乎根本没有被调用,