Support » Plugin: LH User Taxonomies » Listing related Users on taxonomy-term.php

  • Resolved parakeet


    My users are now related to taxonomy “firm”.
    I have now created a theme file for that, taxonomy-firm.php, listing term meta etc.
    It should also display a list and details of the users related to the current term.

    How do I do this?

    So far, I have…

                  $users = get_objects_in_term( $term->term_id, 'firm' );
                  echo '<ol>';
                  foreach( $users as $user ) {
                    echo '<li>User details</li>';
                  echo '</ol>';

    This successfully spits out the correct number of bullet lists corresponding to the user count related to this term.

    But I don’t know how to access the users’ names, descriptions and custom meta fields from here. eg. echo $user->display_name doesn’t output anything. And then there are custom meta fields.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author shawfactor



    returns a list of the ids of the obects in that term, in this case the user ids but for standard taxonomies like categories it would be post ids.

    from their you could use get_user_by

    to get the user object for each id and from there use get_user_meta to get the user meta for each user:

    sorry on my phone so this will have to do

    but if you are ever in any doubt what info a function is returning (lie you were with get_objects_in_term) you can use print_r.

    That’s great. Thanks.

    For reference, here’s my code:

                        $users = get_objects_in_term( $term->term_id, 'firm' );
                        foreach( $users as $user ) {
                          $author_obj = get_user_by("id", $user);
                          echo $author_obj->display_name;

    Now I just need to figure out how to order the output by certain user fields, ie display_name.

    • This reply was modified 11 months, 1 week ago by  parakeet.
    Plugin Author shawfactor


    Do a user query by id and order by display name:


    Look at this to query by the user ids:

    And this to order by the display name:

    I’m on my phone ATM so hints rather than code will have to do but you are on the right track!

    Okay, great. For others’/future reference, here is where I am…

      // get ids of objects related to this term
      $user_ids = get_objects_in_term( $term->term_id, 'firm' );
      // get the users with the ids found above, and sort them
      $user_query = new WP_User_Query( array( 'include' => $user_ids, 'orderby' => 'display_name', 'order' => 'ASC' ) );
      // put user details in to an array
      $users = (array) $user_query->results;
      foreach( $users as $user ) {
        echo get_author_posts_url($user->ID);

    (Actually, I’m omitting some of what I’m echoing, for sake of legibility here).

    So, if I understand this correctly (per my code comments)…

    * You get the IDs of objects related to the term.
    * Then you have to do a WP_User_Query on those results so that you can pass the orderby parameter.
    * You put that in an array.
    * And then you spit out the results for each.

    This all seems to work well for me. Thanks so much. I’m loving learning at the moment.

    In my case, my results were echoed out in ascending order on display_name without even passing orderby since it seems ASC is the default, but passing DESC proves it works.

    Bonus point: if I wanted to order the user list by last_name, I would pass a meta_key and meta_value in to WP_User_Query, like this.

    Bonus question: I’m curious – that first part where I get_objects_in_term( $term->term_id, 'firm' );… is it possible, if that taxonomy is related to more than one object (ie. not just Users but also Posts, CPT1, CPT2, etc), that get_objects_in_term will return things other than Users? If so (1), I’ll rename $user_ids to $object_ids. If so (2), does the WP_User_Query call then have the happy effect of filtering out non-User objects?


    • This reply was modified 11 months, 1 week ago by  parakeet.
    Plugin Author shawfactor


    Yes you are right taxonomies can be linked to users and posts/cpts.

    The ids returns via get_objects_in_term would/could be both post ids and user ids and you could not distinguish which is which. This is inherent in the way wordpress does taxonomies though so it cannot be fixed in my plugin.

    This might cause a problem if you are reusing those ids (for instance a wp_user_query. Although only if there is an Id collision, if you pass an id that doesn’t exist it will be ignored.

    Theoretically you could work around the id collision problem by altering either the posts table or users table so it autoincrements ids starting from a ridiculously high number.

    • This reply was modified 11 months, 1 week ago by  shawfactor.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Listing related Users on taxonomy-term.php’ is closed to new replies.