Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a number of category terms act as placeholders for our departments. I than pull in those terms on the backend profile edit screen using Justin Tadlock's tutorial:

http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields

and the following code by sanchothefat

Now I can list all selected terms by the user on the users author page, but here is my real question and mind breaker:

How can I visit the term page, which acts like a home page for the department, and list all authors that have that term selected in their profile?

Thanks for the suggestions in advance.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

Try the WP_User_Query, something like:

 $term = get_queried_object();

 $users = new WP_User_Query(array(
   'meta_key'   => 'your_meta_key',        // the key in which you store your terms
   'meta_value' => (int)$term->term_id,    // assuming you store term IDs
 ));

 print_r($users->get_results()); 


A update based on your input:

$term = get_queried_object();

$user_query = new WP_User_Query(array(
  'role'         => 'subscriber',         // or whatever your target role is
  'meta_key'     => 'involvement', 
  'meta_compare' => 'like', 
  'meta_value'   => $term->name,          // or "%{$term->name}%" ?
  'fields'       => array('user_email'),  // if you only need the email
 )); 

$users = $user_query->get_results();

foreach($users as $user)
  echo get_avatar($user->user_email);

Just be aware that the "like" query can fail and return you the wrong user in case you have two terms with similar names, like "abc def" and "abc" - in the 2nd case you'll get users with the "abc def" term too.

The only reliable way is to get all users with the meta data (remove meta arguments and change "fields" to "all_with_meta"), then check the meta for the exact term match, but this query is quite expensive...

share|improve this answer
    
Thanks One Trick Pony. That's already much shorter than my approach. However, print_r only shows "Array()" without data. What is get)results supposed to display? I hope to show the authors avatar in the end. –  Marius Ooms Sep 6 '11 at 21:00
    
A array of author objects. I'm not sure how meta data is stored, but if you don't have many users, you could get them all, then get the meta info for each one and check if the term is present in it. –  onetrickpony Sep 6 '11 at 21:11
    
We have close to 250 users. So I think your approach is the right one. The returned array is just not showing results. –  Marius Ooms Sep 6 '11 at 21:41
    
I realized the problem. Your code works on meta with a a single value, however my meta value is an array of data, namely the department names. Users can select multiple departments they are involved in. They are all stored in the same meta value. Any ideas? I'm so super close! Thanks for helping... –  Marius Ooms Sep 6 '11 at 21:53
    
then try storing term slugs instead of IDs, add 'meta_compare' => 'like', and change the meta _value to $term->slug (or "%{$term->slug}%" if that doesn't work) –  onetrickpony Sep 6 '11 at 22:06
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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