capabilities - Add capability to a role , so user is only able to view his own posts

I have created a user role vendor. Right now i am able to give the capabilities to the users,to be able to only edit,d

I have created a user role vendor. Right now i am able to give the capabilities to the users, to be able to only edit,delete etc

But then the user sees all the posts irrespective of who is the owner of the post.

But I was wondering can i somehow add capability to rolevendor so that user is only able to see his posts only...

I have created a user role vendor. Right now i am able to give the capabilities to the users, to be able to only edit,delete etc

But then the user sees all the posts irrespective of who is the owner of the post.

But I was wondering can i somehow add capability to rolevendor so that user is only able to see his posts only...

Share Improve this question asked Jul 29, 2015 at 9:19 terminatorterminator 6151 gold badge6 silver badges30 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

First of all you need to remove capability edit_others_posts if assigned to vendor role.

Then use the code snippet given below:

function posts_for_current_author($query) {
    global $pagenow;

    if( 'edit.php' != $pagenow || !$query->is_admin )
        return $query;

    if( !current_user_can( 'edit_others_posts' ) ) {
        global $user_ID;
        $query->set('author', $user_ID );
    }
    return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');

Above code allows any users with the capability to edit other’s posts to view all posts. Where as users having role Vendor will only see their own posts.

For more customization you can use 'User Role Editor' plugin.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745360798a4624347.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信