wp query – Group users by meta field, with name of meta field as group title
I’m creating a employee photo directory based off users from my site. I’m trying to add front end filtering for this query and I have it mostly working.
What I’m trying to accomplish is to sort the users by last name, department (custom field), or office location (custom field). Those of like departments and office locations would be grouped together under a heading of said department/location.
Everything is working with the exception of listing the department/location name, when using these filters, all the users are grouped and sorted by department/location as expected, but i need to return/echo the name of the department/location for each group of employees.
Here’s how I’m currently querying and filtering
if (!isset($_GET["sortBy"])){
$args = array('orderby' => 'display_name',);
} else {
$sortBy = htmlspecialchars($_GET["sortBy"]);
$args = array(
'meta_key' => $sortBy,
'orderby' => 'meta_value',
);
}
echo 'Sort By: <a href="?sortBy=first_name">First Name</a> // <a href="?sortBy=last_name">Last Name</a> // <a href="?sortBy=emp_dept">Department</a> // <a href="?sortBy=emp_office">Location</a> ';
echo '<ul class="photo_dir">';
$users = get_users($args);
foreach($users as $user){
if (empty($user->next_ad_int_user_disabled)){
$userlogin = $user->user_login ;
$userID = $user->ID ;
$regexCaptures = "/CN=([\w\s\\\\,]+),/";
$manager = $user->emp_manager;
preg_match($regexCaptures, $manager, $match);
List($lastname, $firstname) = explode(',', str_replace("\\", "", $match[1]));
$manager = $firstname . " " . $lastname;
if (empty($user->emp_profile_pic)){
$profile_pic="http://www/wp-content/uploads/2023/08/logo-col-white-space.jpg";
} else {
$profile_pic="data:image/gif;base64," . $user->emp_profile_pic;
}
if (empty($user->emp_mobile_phone )){
$mobile_phone="";
} else {
$mobile_phone="<span><i class="fa-solid fa-mobile-screen-button"></i> " . $user->emp_mobile_phone . '</span>';
}
if (empty(get_field('vehicle_',$userID))){
$vehicle="";
} else {
$vehicle="<span><strong>Vehicle ID:</strong> " . get_field('vehicle_',$userID) . '</span>';
}
echo '<li class="emp_profile" style="max-height:155px;">';
echo '<button class="wp-colorbox-inline cboxElement" href="#inline_content_' . $userlogin . '" style="background-image: url(' . $profile_pic . ');">';
echo '<span>';
echo '<b style="font-size:14px;">' . $user->data->display_name . ' x' . $user->emp_office_ext . '</b><br/>';
echo '<em style="font-size:12px;white-space: nowrap; ">' . $user->emp_position . '</em>';
echo '</span>';
echo '</button>';
echo '<div style="display:none;">';
echo '<div id="inline_content_' . $userlogin . '" class="emp_info">';
echo '<div class="left_half"><img name="Emp_photoHolder" src="' . $profile_pic . '" border="0" alt="Please consult with HR to get your photo added to the intranet. A new picture may need to be taken." style="width:595px;">';
echo '</div><div class="right_half">';
echo '<h3>' . $user->data->display_name . '</h3>';
echo '<span style="text-align: center;font-style: italic;line-height: 10px; margin-bottom: 30px;">' . $user->emp_position . '</span>';
echo '<span><strong>Department:</strong> ' . $user->emp_dept . '</span>';
echo '<span><strong>Manager:</strong> ' . $manager . '</span>';
echo '<i class="fa-solid fa-location-dot"></i> ' . $user->emp_office;
echo $mobile_phone;
echo $vehicle;
echo '<div class="contact_info">';
echo '<div style="display: inline-block;width:59%;"><i class="fa-regular fa-paper-plane"></i> <a href="mailto:' . $user->user_email . '">' . $user->user_email . '</a></div>';
echo '<div style="display: inline-block;width:40%;"><i class="fa-solid fa-phone"></i> 360-565-3<b>' . $user->emp_office_ext . '</b></div>';
echo '</div>';
echo '</div>';
echo '<div style="clear:both;"></div>';
echo '</div>';
echo '</div>';
echo '</li>';
}
}
echo '</ul>';
wp_reset_query();
Leave an answer