Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

4 Answers 4

up vote 6 down vote accepted

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();
}
share|improve this answer
    
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

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

share|improve this answer
    
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

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
}
share|improve this answer
    
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

Have a look here for a more complete solution (to also fix the post count on the filter bar): Help to optimize some working code (to only show posts of logged in Author and fix post count)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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