I have created a taxonomy called "user_category"

I am able to obtain the id of the term from the current logged in user -

$current_user = wp_get_current_user();
$terms = get_the_terms($current_user->ID, 'user_category' );

With the id for this term, I then want to find all other users that have this same term id.

I have the following, however this does not work, it will return a post -

$args = array(
        'tax_query' => array(
                'taxonomy' => 'user_category',
                'field' => 'term_id',
                'terms' => 2 // This being the id 
    $postslist = get_posts( $args );

Can someone help point me in the right direction?

  • not sure what you expect get_posts to return if not a post. On a more basic level it is not very clear what is it that you try to do at all – Mark Kaplun Sep 29 '16 at 15:47
  • Obviously get_posts is not what I require. What I need is a list of users that are assigned to the same tax as the current logged in user. – Rory Standley Sep 29 '16 at 15:52
  • users are not content, you should not use taxonomies with them – Mark Kaplun Sep 29 '16 at 15:53
  • 1
    you can group them with a meta value or role if that is what you want to do – Mark Kaplun Sep 29 '16 at 15:54
  • What is the best way for me to group them then? – Rory Standley Sep 29 '16 at 15:54

You can use Wordpress function get_objects_in_term

$users = get_objects_in_term(2, 'user_category'); // 2 being term id

This return array of users that have this same term id.

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.