Because I’m also storing the user IDs against each company, office and team post object as user taxonomy I can reverse the query and only include the user IDs assigned to that specific post object.
A similar thing to what has been done here – http://mywebsiteadvisor.com/learning/tutorials/building-simple-groups-plugin-for-wordpress-tutorial/step-5/
Only I’ve modified it to be a bit more vigilant in the check before modifying the query.
function preUserQuery($query) {
global $pagenow, $wpdb;
if(is_admin() && $pagenow == 'users.php') {
$ids = array();
foreach(array('userCompany','userOffice','userTeam') as $queryVar) {
if(isset($_GET[$queryVar]) && is_numeric($_GET[$queryVar]) && (int) $_GET[$queryVar] > 0) {
$groupType = get_post_type($_GET[$queryVar]);
$term = get_term_by('name',$_GET[$queryVar],$groupType);
if(is_object($term)) {
$ids = array_merge(get_objects_in_term($term->term_id,$groupType),$ids);
}
}
}
if(!empty($ids)) {
$ids = implode(',',wp_parse_id_list($ids));
$query->query_where .= " AND $wpdb->users.ID IN ($ids)";
}
}
return $query;
}
add_filter('pre_user_query','preUserQuery',9999);