I want to associate certain users by means of a custom key stored in the user_meta for both users. There may be more than one-one relationships, so it'll be in an array. If 2 users have the same key in this _mm_group_membership array, they're part of the group.
Now since meta_value queries are restricted to simple comparisons (=, <, >, etc.) here's what I'm proposing, searching for users related to $user_id on the basis of group membership $key:
function linked_users_array ($user_id, $key) {
$group = '_mm_group_membership';
$users = get_users('meta_key='.$group.'&fields=all_with_meta');
$linked_user_ids = array();
foreach ($users as $u) {
if (in_array($key, $u->$group)) {
$linked_user_ids[] = $u->ID;
}
}
return $linked_user_ids;
}
2 questions on this:
1) Is $u->$group possible? Or do I need to use something along the lines of var_dump( $u->get( $group ) );
?
2) I want to minimise hitting the database, so I don't really want an extra get_user_meta() call on each user in the loop. Is this possible?
Thanks in advance.