0

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

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

1 Answer 1

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.

1
  • 1
    You are a diamond mate. It worked like a charm. Thank you so much Commented Dec 31, 2011 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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