我如何才能禁止我的站点上的贡献者查看该站点上发布的其他帖子,而只查看他们自己的帖子?
贡献者禁用查看他人的帖子
3 个回复
最合适的回答,由SO网友:PrivateUser 整理而成
我希望你说的是wp管理部门。如果是,只需将此代码放入functions.php
文件
add_action( \'load-edit.php\', \'posts_for_current_contributor\' );
function posts_for_current_contributor() {
global $user_ID;
if ( current_user_can( \'contributor\' ) ) {
if ( ! isset( $_GET[\'author\'] ) ) {
wp_redirect( add_query_arg( \'author\', $user_ID ) );
exit;
}
}
}
SO网友:user1974153
@女孩:谢谢你的回答,但还不完美。
管理员可以手动修改查询参数“author”,并且可以查看其他参数
if ( ! isset( $_GET[\'author\'] ) ) {
上面的行还应检查$\\u GET[\'author\']是否不是当前用户SO网友:T.Todua
另一种方法,如接受的答案(但无重定向):
function posts_for_current_author ($query) {
if( $query->is_admin && \'edit.php\' == $GLOBALS[\'pagenow\'] && !current_user_can( \'edit_others_posts\' ) ) {
$query->set(\'author\', $GLOBALS[\'user_ID\'] );
}
return $query;
};
add_filter(\'pre_get_posts\', \'posts_for_current_author\');
结束