1

I've successfully created 3 taxonomies attached to the user object, using the registered_taxonomy and update_count functions of this code - https://gist.github.com/portfola/10750784#file-user-taxonomies-php (I don't need the rest as they're only relevant for taxonomies displaying an admin UI or taxonomy archive which I'm not). These taxonomies store which company, office and teams, each user is assigned to.

I know the custom taxonomies are storing the data as I get multiple results back via the wp_get_object_terms($user->ID,'company') function.

What I need is to be able to filter the users.php results by either one of or multiple of these custom taxonomies. I've created the additional form inputs found at the WP_List_Table and have also added the columns for each taxonomy. The only thing left to do is modify the pre_user_query. I don't think this is the right way to do as it's not having any effect on the results returned at all and I'm only beginner of MySQL.

Here's my current code for modifying the query:

function preUserQuery($query) {
    global $pagenow, $wpdb;
    if(is_admin() && $pagenow == 'users.php') {
        $query->query_vars['tax_query'] = array();
        foreach(array('userCompany','userOffice','userTeam') as $queryVar) {
            if(isset($_GET[$queryVar]) && is_numeric($_GET[$queryVar])) {
                $query->query_vars['tax_query'][] = array(
                    'taxonomy' => get_post_type($_GET[$queryVar]),
                    'field' => 'name',
                    'terms' => $_GET[$queryVar]
                );
            }
        }
    }
    return $query;
}
add_filter('pre_user_query','preUserQuery',9999);

1 Answer 1

0

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);

Your Answer

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

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