Title: getExcludedPosts for a specific user
Last modified: May 30, 2017

---

# getExcludedPosts for a specific user

 *  Resolved [patsam](https://wordpress.org/support/users/patsam/)
 * (@patsam)
 * [9 years ago](https://wordpress.org/support/topic/getexcludedposts-for-a-specific-user/)
 * Hi
    Is there a way to getExcludedPosts for a specific user?
 * e.g. getExcludedPosts($user_id)
 * If not could you suggest a way to extend the relevant class so I could create
   my own function? e.g. getExcludedPostsByUser()
 * I am using the latest version 2.0.11
 * Thanks in advance.
 * BTW I really appreciate your plugin it’s fantastic

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [gm_alex](https://wordpress.org/support/users/gm_alex/)
 * (@gm_alex)
 * [8 years, 12 months ago](https://wordpress.org/support/topic/getexcludedposts-for-a-specific-user/#post-9232931)
 * Use the following code to get the excluded post for the current user. If you 
   need it for a specified user have a look here: [https://github.com/GM-Alex/user-access-manager/blob/master/src/UserAccessManager/AccessHandler/AccessHandler.php#L502](https://github.com/GM-Alex/user-access-manager/blob/master/src/UserAccessManager/AccessHandler/AccessHandler.php#L502)
 *     ```
       global $userAccessManager;
       $userAccessManager->getAccessHandler()->getExcludedPosts();
       ```
   
 *  Thread Starter [patsam](https://wordpress.org/support/users/patsam/)
 * (@patsam)
 * [8 years, 11 months ago](https://wordpress.org/support/topic/getexcludedposts-for-a-specific-user/#post-9267917)
 * Hi Alex,
 * Thanks for the feedback. This is what I have done to do what I need
 * **Some background to what I am trying to do:**
    I simply want to create a list
   of all users and show or count which posts that each user can or cannot access.
 * In the end I changed the AccessHandler.php source and created some new functions
   which allows me to provide a user as opposed to the default current user
 * See code below, this works for me but for the next version it would be nice if
   the we could call **getExcludedPosts()** using a specified user and include arguments
   so we could exclude checking of IPaddress and maybe some other settings (can’t
   think of others at the moment)
 * Thank you for all your work on this plugin and would be more than happy to contribute
   as needed
 * Regards Pat (see my code below)
 *     ```
           public function checkUserAccess_tko($oCurrentUser,$allowedCapability = false)
           {
               $currentUser = $oCurrentUser;
   
               if ($this->wordpress->isSuperAdmin($currentUser->ID) === true
                   || $allowedCapability !== false && $currentUser->has_cap($allowedCapability) === true
               ) {
                   return true;
               }
   
               $roles = $this->getUserRole($currentUser);
               $rolesMap = array_flip($roles);
   
               $orderedRoles = [UserGroup::NONE_ROLE, 'subscriber', 'contributor', 'author', 'editor', 'administrator'];
               $orderedRolesMap = array_flip($orderedRoles);
   
               $userRoles = array_intersect_key($orderedRolesMap, $rolesMap);
               $rightsLevel = (count($userRoles) > 0) ? end($userRoles) : -1;
               $fullAccessRole = $this->config->getFullAccessRole();
   
               return (isset($orderedRolesMap[$fullAccessRole]) === true && $rightsLevel >= $orderedRolesMap[$fullAccessRole]
                   || isset($rolesMap['administrator']) === true
               );
           }
   
           public function getExcludedPosts_tko($user)
           {
               /**
                * Call this in TKO like this ,
                *    $excluded_posts = $userAccessManager->getAccessHandler()->getExcludedPosts_tko(get_user_by('ID',$user_id));
                */
   
               /*
               if ($this->checkUserAccess('manage_user_groups')) {
                   // if you have these capabilities then get out as you can access everything
                   $this->excludedPosts = [];
               }
               */
   
               if ($this->checkUserAccess_tko($user,'manage_user_groups')) {
                   // if you have these capabilities then get out as you can access everything
                   $ret_excludedPosts = [];
               }
   
               if ($ret_excludedPosts === null) {
                   $excludedPosts = [];
   
                   // get all user groups
                   $userGroups = $this->getUserGroups();
   
                   // get all groups that the current user belongs to
                   $userUserGroups = $this->getUserGroupsForUser_tko( isset($user) ? $user : null);
   
                   // place all posts assigned to each user group into excludedPosts array
                   foreach ($userGroups as $userGroup) {
                       $excludedPosts += $userGroup->getFullPosts();
                   }
   
                   // Subtract from the excludedPosts array, all posts that belong to groups that the current user belongs to
                   foreach ($userUserGroups as $userGroup) {
                       $excludedPosts = array_diff_key($excludedPosts, $userGroup->getFullPosts());
                   }
   
                   // if not in wp-admin (e.g. currently the front end) then
   
                   $noneHiddenPostTypes = [];
                   $postTypes = $this->objectHandler->getPostTypes();
   
                   foreach ($postTypes as $postType) {
                       if ($this->config->hidePostType($postType) === false) {
                           $noneHiddenPostTypes[$postType] = $postType;
                       }
                   }
   
                   foreach ($excludedPosts as $postId => $type) {
                       if (isset($noneHiddenPostTypes[$type]) === true) {
                           unset($excludedPosts[$postId]);
                       }
                   }
   
                   $postIds = array_keys($excludedPosts);
                   $ret_excludedPosts = array_combine($postIds, $postIds);
               }
   
               return $ret_excludedPosts;
           }
   
           /**
            * Returns the user groups for the user.
            *
            * @return UserGroup[]
            */
           public function getUserGroupsForUser_tko($user, $access='read' )
           {
               /**
                * * TKO Change - was
                * *    Original had no args
                */
   
               if ($this->checkUserAccess_tko($user,'manage_user_groups') === true ) {
                   return $this->getUserGroups();
               }
   
               if ($this->userGroupsForUser === null) {
                   /**
                    * TKO Change - was
                    *   $currentUser = $this->wordpress->getCurrentUser();
                    */
                   $currentUser = isset($user)? $user: $this->wordpress->getCurrentUser();
                   $userGroupsForUser = $this->getUserGroupsForObject(
                       ObjectHandler::GENERAL_USER_OBJECT_TYPE,
                       $currentUser->ID
                   );
   
                   $userGroups = $this->getUserGroups();
   
                   /**
                    * This next one can catch you out becuase
                    *  - if the user is currently editing a document (e.g. $this->config->atAdminPanel() === true )
                    *  - and a certain group/s allows write access (e.g. $userGroup->getWriteAccess() === 'all' )
                    *  - then it will distort the excludedposts_tko result
                    *
                    *  NOTE THIS ONLY AFFECTS - getexcludedposts_tko
                    */
   
                   $is_admin =  ( $this->config->atAdminPanel() === true );
   
                   foreach ($userGroups as $userGroup) {
   
                       $_this_userGroup_is_notin_assigned_user_groups = ( isset($userGroupsForUser[$userGroup->getId()]) === false );
   
                       $but_read_access_is_allowed = ( $userGroup->getReadAccess() === 'all' );
   
                       $but_write_access_is_allowed = ( $userGroup->getWriteAccess() === 'all' );
   
                       $it_must = $_this_userGroup_is_notin_assigned_user_groups;
   
                       /*
                       $_this_IP_is_allowed = ( $this->isIpInRange($_SERVER['REMOTE_ADDR'], $userGroup->getIpRangeArray()) );
   
                       $condition_1_or = $_this_IP_is_allowed;
   
                       $condition_2_or = ( ! $is_admin && $but_read_access_is_allowed );
   
                       $condition_3 = ( $is_admin && $but_write_access_is_allowed );
                       */
   
                       if($access == 'read'){
                           $add_to_usergroup = $it_must && ( $but_read_access_is_allowed  );
                       } else {
                           $add_to_usergroup = $it_must && ( $but_write_access_is_allowed );
                       }
   
                       /*if (isset($userGroupsForUser[$userGroup->getId()]) === false
                           && ($this->isIpInRange($_SERVER['REMOTE_ADDR'], $userGroup->getIpRangeArray())
                               || $this->config->atAdminPanel() === false && $userGroup->getReadAccess() === 'all'
                               || $this->config->atAdminPanel() === true && $userGroup->getWriteAccess() === 'all')
                       ) */
   
                       if($add_to_usergroup)
                       {
                           $userGroupsForUser[$userGroup->getId()] = $userGroup;
                       }
                   }
   
                   $this->userGroupsForUser = $userGroupsForUser;
               }
   
               return $this->userGroupsForUser;
           }
   
       }
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘getExcludedPosts for a specific user’ is closed to new replies.

 * ![](https://ps.w.org/user-access-manager/assets/icon.svg?rev=1563783)
 * [User Access Manager](https://wordpress.org/plugins/user-access-manager/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/user-access-manager/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/user-access-manager/)
 * [Active Topics](https://wordpress.org/support/plugin/user-access-manager/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/user-access-manager/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/user-access-manager/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [patsam](https://wordpress.org/support/users/patsam/)
 * Last activity: [8 years, 11 months ago](https://wordpress.org/support/topic/getexcludedposts-for-a-specific-user/#post-9267917)
 * Status: resolved