Function to hide pages from the editor view on the dashboard of WordPress per user capability

I’m building a multi-user WordPress environment. The users are divided in groups with certain privileges. The privileges mostly concerns the backend, or the dashboard of WordPress. The purpose is to restrict the users’ ability to edit certain pages.

It’s straightforward to control user’s abilities with a role and capability plugin like Members. It’s not possible to control the user’s abilities in the dashboard with Members however. In that case it’s useful to create specific filters for the groups which show just the pages a group can edit.

I tried a couple of plugins, but most of them are about hiding pages and posts on the public site. The few for the dashboard like Manage your posts only show the posts an author has written for the author only. In my case the author is another person, so that’s not applicable.

By using the code from the Manage your posts plugin (It’s really just a function you can include in your functions.php) as a base I was able to make it show only certain page or pages to certain user groups. The capabilities have been set with Members.

function edit_page_per_capability($query) {
if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit-pages.php' ) !== false ) {
   if (current_user_can('organizer')) {
      $query->set('post__in', array(184,186) );
   }
   elseif ( current_user_can( 'instructor' ) ) {
      $query->set('page_id', '186');
   }
   elseif ( current_user_can( 'office' ) ) {
      $query->set('page_id', '8');
   }
}
}
add_filter('pre_get_posts','edit_page_per_capability');

The numbers point to specific pages which the user groups are supposed to edit. On the page list the groups see only what they are allowed to. For more options check out query_posts from the WordPress Codex. With a minimal effort you can have it to point to pages by their real names, so the code becomes more descriptive.

For WordPress 3.0 and onwards you need to change the URL in the code (it should point to Pages in the dashboard menu). The URL is ‘/wp-admin/edit.php?post_type=page’.

You can use the code on your page by adding it to your functions.php file and modifying it to suit your purposes.

One thought on “Function to hide pages from the editor view on the dashboard of WordPress per user capability