5

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".

  • 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 Apr 8 '11 at 8:58
  • @Jan This is in the admin side. – Chuck Apr 8 '11 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 Apr 11 '11 at 9:41
11

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:

add_action( 'load-edit.php', 'wpse14230_load_edit' );
function wpse14230_load_edit()
{
    add_action( 'request', 'wpse14230_request' );
}

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();
}
  • Thank You, that is what I wanted to do, for some reason I was thinking that clicking a post after clicking All would show the admin side, not the public side, I should have looked closer but I like also hiding (Mine, All, Drafts) – Chuck Apr 13 '11 at 0:40
1

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

  • Indeed, you can't see the detail of a post, but you can still see the other posts in the overview. If you display meta information in the overview this can be a problem. – Jan Fabry Apr 11 '11 at 9:42
1

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
}
  • Sorry, I should have been more specific, this is for the admin side. – Chuck Apr 8 '11 at 22:05
  • @Chuck: If you display your meta information in the post list on the admin side, you can still use this method to only display the meta info for posts that the current user should be able to see, and display an empty cell for the others. – Jan Fabry Apr 11 '11 at 10:01
1

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.