最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论