admin管理员组

文章数量:1122846

I am setting up a site where there will be multiple users as Author, the owner doesn't want the authors to be able to view each others posts, since there are some meta fields with information he'd rather not have shared between the authors.

Is there a way to remove the ability to view other authors posts?

Thanks, Chuck

To clarify a bit more, this is for the admin side, at the top underneath Posts, there are links for mine, all, and published. I only want Authors to see "mine".

I am setting up a site where there will be multiple users as Author, the owner doesn't want the authors to be able to view each others posts, since there are some meta fields with information he'd rather not have shared between the authors.

Is there a way to remove the ability to view other authors posts?

Thanks, Chuck

To clarify a bit more, this is for the admin side, at the top underneath Posts, there are links for mine, all, and published. I only want Authors to see "mine".

Share Improve this question edited Apr 8, 2011 at 22:14 Chuck asked Apr 8, 2011 at 0:53 ChuckChuck 5231 gold badge9 silver badges24 bronze badges 3
  • Are you talking about the admin side or the public side of your website? (Use @Jan when you reply in a comment and I get a notification) – Jan Fabry Commented Apr 8, 2011 at 8:58
  • @Jan This is in the admin side. – Chuck Commented Apr 8, 2011 at 21:58
  • Viewing the details (the edit screen) of someone else's post should be impossible if you are an Author, but you can still see the posts in the overview table. Is it this overview that you want to hide too? (Where do you display the meta fields?) – Jan Fabry Commented Apr 11, 2011 at 9:41
Add a comment  | 

5 Answers 5

Reset to default 15

If you want to prevent a user with the "Author" role to view other users' posts in the overview screen (they won't be able to view the details anyway), you can add an extra filter on the author:

// Only add the filter on the edit.php admin page
add_action( 'load-edit.php', 'wpse14230_load_edit' );
function wpse14230_load_edit()
{
    add_action( 'request', 'wpse14230_request' );
}

// The actual filter
function wpse14230_request( $query_vars )
{
    if ( ! current_user_can( $GLOBALS['post_type_object']->cap->edit_others_posts ) ) {
        $query_vars['author'] = get_current_user_id();
    }
    return $query_vars;
}

The little links above the post table ("Mine", "All", "Drafts") are less useful now, you can also remove them:

add_filter( 'views_edit-post', 'wpse14230_views_edit_post' );
function wpse14230_views_edit_post( $views )
{
    return array();
}

I had to do something like this today and this is why I found this post. What I found that worked for me was this post titled: "How to Limit Authors to their Own Posts in WordPress Admin" by wpbeginner

Here is the code that you can paste on your functions.php:

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');

That is exactly what the default "author" role does. http://codex.wordpress.org/Roles_and_Capabilities

Just check for capabilities (See link from @Wyck) & the author ID inside your templates and put the stuff you don't want others to see inside an if/else check:

// Get the author of this post:
$post_author = get_query_var('author_name') ? get_user_by( 'slug', get_query_var('author_name') ) : get_userdata( get_query_var('author') );

// Get data from current user:
global $current_user;
get_currentuserinfo();
// Get the display_name from current user - maybe you have to exchange it with $current_user->user_login
$current_author = $current_user->display_name;

// Check the capability and if the currently logged in user is the the post author
if ( current_user_can('some_capability') && $post_author == $current_author )
{
    // Post Meta
    $post_meta = get_post_meta( $GLOBALS['post']->ID );
    // DO OR DISPLAY STUFF HERE
}

Have a look here for a more complete solution (to also fix the post count on the filter bar): Help to condense/optimize some working code

本文标签: Prevent Authors from viewing each others Posts