WordPress.org

Ready to get started?Download WordPress

Forums

User Taxonomies
How to query for users? (4 posts)

  1. Yaron Guez
    Member
    Posted 1 year ago #

    Hi!
    I'm thinking about using this plugin for a project I'm working on instead of relying on user meta. I'm wondering how you would query users by taxonomy. The basis for this plugin, http://justintadlock.com/archives/2011/10/20/custom-user-taxonomies-in-wordpress, describes using the function get_objects_in_term() but I wonder if that is the only way. Could you instead use get_users so that you could search for a user based on taxonomy AND meta/role/search/etc?

    http://wordpress.org/extend/plugins/user-taxonomies/

  2. Damian Gostomski
    Member
    Plugin Author

    Posted 1 year ago #

    Hi

    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?

  3. gothickmatt
    Member
    Posted 1 year ago #

    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 )

  4. Yaron Guez
    Member
    Posted 1 year ago #

    Interesting approach. Thanks for the tip!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.