1

I've been trying to find a way to limit the user from seeing some pages in the "pages" menu in the admin panel.

I looked in the edit.php file and noticed:

} elseif ( 'page' == $post_type ) {

However, i am unsure of what i need to edit in order to show some pages and hide others depending on the role.

The Admin will be able to see all posts.

The SubAdmin will only be able to see some pages.

How can i edit the edit.php file (or some other php file) in order to do this?

1 Answer 1

2

To get the current role of the user

$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
   return;
$roles = $current_user->roles;  //$roles is an array

After getting role set page ids which you want to show according to roles (for example)

    if($roles=='administrator'){
     $args=array('21','22','23');
    }

   or

   if($roles=='subscriber'){
     $args=array('24','25','26');
    }

you can use parse_query filter hook to exclude your pages using post__not_in attribute

add_filter( 'parse_query', 'exclude_pages_from_admin' );
function exclude_pages_from_admin($query) {
    global $pagenow,$post_type;
    if (is_admin() && $pagenow=='edit.php' && $post_type =='page') {
        $query->query_vars['post__not_in'] = $args
    }
}

Important Links:

1
  • Great, I was looking for a solution like this one. Basically hide posts and pages in the dashboard depending on role and post category. A few questions: will this work in WP 4.5.2? Where do I have to modify this and what will happen on updates? Thanks a lot
    – Riccardo
    Commented May 23, 2016 at 8:17

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.