I have a custom taxonomy for users which is country. Is there a way to query all admins by term_id or should I get all users and check each one with a for loop?

I tried this:

$user_query = new WP_User_Query( array (  'role' => 'administrator',  'tax_query' => array(array('taxonomy' => 'country', 'field' => 'id', 'terms' => $country_id ) )  ) );

Not sure why you would try this with taxonomy.

I would suggest making a meta_key "country" with the values.

Then you can query it like in the documentation:

$user_query = new WP_User_Query( array( 'meta_key' => 'country', 'meta_value' => 'Israel' ) );

Not sure why your taxonomy-query doesnt work, looks fine also, i use it like this for my post-types.

How did you try? Maybe also post the function, where you register the taxonomy, where did you attach it to?

  • thanks, I am using a taxonomy because I assign the country to several types of objects (user, post, custom tax, discussions). I wrote the function in the same place all other funcs are being called from. – Meschiany Sep 16 '14 at 13:30
  • you have to attach your taxonomy to the post_types and object types. That´s the code i would like to see. Can you see your taxonomies in the backend? are you sure the taxonomies work for user? have a look here: justintadlock.com/archives/2011/10/20/… and here: codex.wordpress.org/Function_Reference/register_taxonomy – vozer Sep 16 '14 at 13:47
  • Taxonomies are working well for me, i can see it in the backend, I used it many times in and got the custom fields etc. no problem with the tax themselves only I dont know how to query a user by them. which code do you want to see? I dont mind posting it. – Meschiany Sep 16 '14 at 14:06
  • 1
    I probably didn't understand you correctly, I did not know that user saves taxonomy as meta as oppose to post. it worked. – Meschiany Sep 16 '14 at 15:50
  • it´s not what i meant but i am glad it worked, now i also learned something:) can you pls edit your question with the right code? – vozer Sep 16 '14 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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