0

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();

1 Answer 1

1

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:

  1. Add this above/before the line foreach($users as $user){:

    $current_group = ''; // The current location/department name.
    $sort_by_meta  = $args['meta_key'] ?? '';
    
  2. 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().

2
  • That was easier than I thought. Thank you!
    – NW Tech
    Commented Sep 26, 2023 at 22:49
  • Glad it helped, and I should add that, if the HTML is going to be exactly the same for both the location and department, you could combine the conditions into one if - if ( $sort_by_meta && ( $user->$sort_by_meta !== $current_group ) && in_array( $sort_by_meta, array( 'emp_office', 'emp_dept' ) ) ) { $current_group = $user->$sort_by_meta; echo ... } - see the full syntax-highlighted code here.
    – Sally CJ
    Commented Sep 27, 2023 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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