Support » Plugins and Hacks » User Taxonomies » How to query for users?

How to query for users?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Damian Gostomski



    Just had a quick look at the WP_User_Query class (which is what powers get_users) and there is no mention of taxonomies or terms as far as I can see, so it would seem not.

    What I’ve actually done in a recent project, which makes user taxonomies redundant, is use a CPT for user data, and link this back to the user.
    This way you get to utilise all the goodness of Custom Post Types and Custom taxonomies, which made everything much easier in my case.

    Not sure how feasible something like that would be for you?

    Incidentally, for anyone still looking for this, the way I found to do it is to use wp_query’s tax_query object (a WP_Tax_Query) to generate SQL that you can then use against arbitrary objects, like Users.

    I’m using the Query Multiple Taxonomies plugin to generate my taxonomy queries, but I think this should work with any taxonomy query. Example code from my taxonomy.php to grab all user_ids for users matching the current taxonomy query:

    extract($wp_query->tax_query->get_sql( $wpdb->users, 'ID' ));
    $user_ids = $wpdb->get_col( "SELECT $wpdb->users.ID FROM $wpdb->users $join WHERE 1=1 $where" );

    At this point, you could, of course, grab anything you liked from the users table, etc., by altering this query, or, more clumsily, I suppose, just call get_userdata() or whatever on each of the user_ids you get back.

    (This method was partly inspired by the tax_query snippet found here: http://wordpress.stackexchange.com/questions/16393/show-certain-terms-from-custom-taxonomy-but-exclude-parent-terms/16795#16795 )

    Interesting approach. Thanks for the tip!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to query for users?’ is closed to new replies.