I am using WP_User_Query to display users by role. This works fine.
I also have attached a taxonomy to each user, by using this plugin: https://en-gb.wordpress.org/plugins/lh-user-taxonomies/
The last step I need help with is connecting this up, by passing an array of terms I would like to filter the loop with.
For example, I would like to list only members with a 'valid membership' term.
-- Code --
$args = array
(
'role' => 'member'
);
$user_query = new WP_User_Query($args);
if ( ! empty( $user_query->results ) ):
foreach ( $user_query->results as $user ):
$user_info = get_userdata($user->ID); // get all the user's data ?>
<?php echo $user->display_name;
endforeach;
endif;
Reigel Gallarde answers:
We can get the id of all users by terms using get_objects_in_term.
then we can use `include` parameter in $args to filter out by its role.
the code will look like this:
$taxonomy_term_ids = array(227, 226, 225);
// assumed term_id 227, 226, 225, for terms Full, Pending, Expired respectively.
$users = get_objects_in_term( $taxonomy_term_ids , 'valid-membership-taxonomy' );
$args = array(
'role' => array(
'editor',
),
'include' => $users
);
$user_query = new WP_User_Query($args);
if ( ! empty( $user_query->results ) ):
foreach ( $user_query->results as $user ):
$user_info = get_userdata($user->ID); // get all the user's data
echo $user->display_name . '<br/>';
endforeach;
endif;
for readability of the code, I've pasted it on pastebin: https://pastebin.com/raw/0q9BTpCp
At the very least, you need to edit these:
$taxonomy_term_ids = array(227, 226, 225);
change 227, 226, 225 according to your term ids. you may have one or more ids in there.
make sure 'valid-membership-taxonomy' is a valid taxonomy. It will give error if not.
$users = get_objects_in_term( $taxonomy_term_ids , 'valid-membership-taxonomy' );
change also the role, as I have set it to 'editor' for testing purpose.
Keith Donegan comments:
Perfect, thanks Reigel
Reigel Gallarde comments:
You're welcome!
Reigel Gallarde comments:
Please don't forget to vote for an accepted answer. Thank you.
Luis Abarca answers:
Maybe this can works.
$args = array('role' => 'member');
$user_query = new WP_User_Query($args);
if ( ! empty( $user_query->results ) ):
foreach ( $user_query->results as $user ):
$user_info = get_userdata( $user->ID ); // get all the user's data
// Get user assigned terms
$terms = wp_get_object_terms( $user->ID, 'valid-membership-taxonomy' )
// User is not on valid membership taxonomy
if ( empty( $terms ) ) {
continue;
}
echo $user->display_name;
endforeach;
endif;
Keith Donegan comments:
Hi Luis, this is on the right track I think, but how would I filter the users by one or more of my below three terms?
Taxonomy: valid-membership-taxonomy
Term 1: Full
Term 2: Pending
Term 3: Expired
Luis Abarca comments:
Maybe this can works.
$args = array('role' => 'member');
$user_query = new WP_User_Query($args);
if ( ! empty( $user_query->results ) ):
foreach ( $user_query->results as $user ):
$user_info = get_userdata( $user->ID ); // get all the user's data
// Get user assigned terms
$terms = wp_get_object_terms( $user->ID, 'valid-membership-taxonomy' )
// User is not on valid membership taxonomy
if ( empty( $terms ) ) {
continue;
}
// User has a membership, users has only one category at the time??
switch ( $terms[0]->term_id ) {
// Example: ID for full term
case 1:
break;
// Example: ID for pending term
case 2:
break;
// Example: ID for expired term
case 3:
break;
}
echo $user->display_name;
endforeach;
endif;