How can I group and order a query by a custom field?

Question

I’m doing a user query and looking to query users that are in a certain department — this works. Now I’m trying to manipulate the order….I’ld like to use the meta_key for position and list users in this order: Managers, Supervisors, Foreman, Coordinator, everyone else

I’ve tried a couple different things with varied results. The one that seemed most promising was this:

$title = get_the_title();
$args = array(  
    'meta_query' => array(
        'relation' => 'OR',
        array(
            array(
                'relation' => 'AND',
                'key' => 'emp_dept',
                'value' => $title,
                'compare' => 'LIKE',
            ),
            'manager_clause' => array(
                'key' => 'emp_position',
                'value' => 'Manager',
                'compare' => 'LIKE',
            ),
        ),
        array(
            array(
                'relation' => 'AND',
                'key' => 'emp_dept',
                'value' => $title,
                'compare' => 'LIKE',
            ),              
            'supervisor_clause' => array(
                'key' => 'emp_position',
                'value' => 'Sup',
                'compare' => 'LIKE',
            ), 
        ),
        array(
            array(
                'relation' => 'AND',
                'key' => 'emp_dept',
                'value' => $title,
                'compare' => 'LIKE',
            ),              
            'foreman_clause' => array(
                'key' => 'emp_position',
                'value' => 'Foreman',
                'compare' => 'LIKE',
            ), 
        ),
        array(
            array(
                'relation' => 'AND',
                'key' => 'emp_dept',
                'value' => $title,
                'compare' => 'LIKE',
            ),              
            'coordinator_clause' => array(
                'key' => 'emp_position',
                'value' => 'Coordinator',
                'compare' => 'LIKE',
            ), 
        ),
    ),
    'orderby' => array( 
        'manager_clause' => 'DESC',
        'supervisor_clause' => 'DESC',
        'foreman_clause' => 'DESC',
        'coordinator_clause' => 'DESC',
    ),
);
$users = get_users($args); 

This ends up just bogging down the page and it never loads. If I reduce the query to just checking for managers and supervisors, it works….it only shows those positions, but it works.

Another variation I tried was:

$title = get_the_title();
$args = array(  
    'meta_query' => array(
            array(
                'relation' => 'and',
                'key' => 'emp_dept',
                'value' => $title,
                'compare' => 'LIKE',
            ),
            'position_clause' => array(
                'relation' => 'or',
                array (
                    'key' => 'emp_position',
                    'value' => 'Manager',
                    'compare' => 'LIKE',
                ),
                array(
                'key' => 'emp_position',
                'value' => 'Sup',
                'compare' => 'LIKE',
                ), 
                array(
                'key' => 'emp_position',
                'value' => 'Foreman',
                'compare' => 'LIKE',
                ), 
                array(
                'key' => 'emp_position',
                'value' => 'Coordinator',
                'compare' => 'LIKE',
                ), 
            ),
    ),
    
    'orderby' => array( 
        'position_clause' => 'DESC',
    ),
);
$users = get_users($args);

This gave me only the users with he requested positions, however, they were ordered by username, not by the hierarchical order of the position I was hoping for.

Is there a better way to accomplish this?

0
NW Tech 3 months 2023-09-19T17:27:10-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse