i need to return/echo the name of the department/location for each
group of employees
That is actually a generic PHP/programming question, nonetheless, if you mean an output which looks like so:
Location/Department Name
- Employee/user
- Employee/user
- Employee/user
Location/Department Name
- Employee/user
- Employee/user
Then here’s one way of accomplishing that:
-
Add this above/before the line
foreach($users as $user){
:$current_group = ''; // The current location/department name. $sort_by_meta = $args['meta_key'] ?? '';
-
Then add this below/after the line
if (empty($user->next_ad_int_user_disabled)){
:Just modify the HTML to your own liking, but note that you need to properly escape data you’re displaying to the user.
// If sorting by location or department, and the current location/department has // changed, close the current UL, display the location name and open a new UL. if ( 'emp_office' === $sort_by_meta && $user->emp_office !== $current_group ) { $current_group = $user->emp_office; echo '</ul>'; echo '<h3>' . esc_html( $current_group ) . '</h3>'; echo '<ul class="photo_dir">'; } elseif ( 'emp_dept' === $sort_by_meta && $user->emp_dept !== $current_group ) { $current_group = $user->emp_dept; echo '</ul>'; echo '<h3>' . esc_html( $current_group ) . '</h3>'; echo '<ul class="photo_dir">'; }
Side Note: I don’t know why did you call wp_reset_query()
, but if you’ve created a custom loop and you wanted to restore the $post
global to the current post in the main query, then call wp_reset_postdata()
and not wp_reset_query()
.