WordPress Role Capability Restriction

The following excludes posts not written by the user from the edit-post screen (you can add further screen IDs if necessary) if they have the ‘contributor’ role.

Normally I would suggest adding a custom capability, and comparing the capability not the role. But since it seems this plug-in isn’t for distribution, this is isn’t such an important distinction.

add_action( 'pre_get_posts', 'wpse111322_hide_posts_not_by_me' );
function wpse111322_hide_posts_not_by_me( $query ){

    global $wp_roles;

    //Which screen IDs to apply to:
    $screen_ids = array( 'edit-post' );

    if( $query->is_main_query() && is_admin() && in_array( get_current_screen()->id, $screen_ids ) ){

        //Get the user's role
        $current_user = wp_get_current_user();
        $roles = $current_user->roles;
        $role = array_shift( $roles );

        if( $role == 'contributor' )
            $query->set( 'author', get_current_user_id() );
    }
}

Note: This doesn’t remove the post count. So you may see All(61) at the top but only 5 posts in the table.