admin管理员组

文章数量:1125544

I would like to display only authors own posts in dashboard all posts section. As of now it displays everything.

I found some code here which is written by @t31os Its working correctly.

function posts_for_current_author($query) {
    global $user_level;

    if($query->is_admin && $user_level < 5) {
        global $user_ID;
        $query->set('author',  $user_ID);
        unset($user_ID);
    }
    unset($user_level);

    return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');

But user_level is deprecated. So can anyone modify the code for new version? Thanks

I would like to display only authors own posts in dashboard all posts section. As of now it displays everything.

I found some code here which is written by @t31os Its working correctly.

function posts_for_current_author($query) {
    global $user_level;

    if($query->is_admin && $user_level < 5) {
        global $user_ID;
        $query->set('author',  $user_ID);
        unset($user_ID);
    }
    unset($user_level);

    return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');

But user_level is deprecated. So can anyone modify the code for new version? Thanks

Share Improve this question asked Dec 31, 2011 at 17:53 PrivateUserPrivateUser 3,46910 gold badges54 silver badges85 bronze badges 7
  • Are you aware of the Roles and Capabilities that wordpress offers by default? Especially Author - Somebody who can publish and manage their own posts – hakre Commented Dec 31, 2011 at 18:21
  • Yes i'm aware of roles and caps. But i'm trying to give permissions to submit content for all registered users (subscribers). But I don't want to list all posts – PrivateUser Commented Dec 31, 2011 at 18:29
  • Okay, I see. As $user_level is deprecated, you might be able to check for it's role(s) then instead. – hakre Commented Dec 31, 2011 at 18:36
  • Can you give me the code? I tried but its not working. – PrivateUser Commented Dec 31, 2011 at 19:15
  • You only need to replace the $user_level with the appropriate role, see Roles and Capabilities: User Levels which level relates to which role. – hakre Commented Dec 31, 2011 at 19:20
 |  Show 2 more comments

1 Answer 1

Reset to default 5

The following worked for me to show only the current user's posts in the admin

add_action( 'load-edit.php', 'posts_for_current_author' );
function posts_for_current_author() {
    global $user_ID;

    /*if current user is an 'administrator' do nothing*/
    //if ( current_user_can( 'add_users' ) ) return;

    /*if current user is an 'administrator' or 'editor' do nothing*/
    if ( current_user_can( 'edit_others_pages' ) ) return;

    if ( ! isset( $_GET['author'] ) ) {
        wp_redirect( add_query_arg( 'author', $user_ID ) );
        exit;
    }

}

It can be edited pretty easily if the restriction should only happen for users in a particular role.

本文标签: deprecationDisplay only author posts in dashboard all posts panel