I'm doing a user query and looking to query users that are in a certain department (custom field) -- this works. Now I'm trying to manipulate the order....I'd like to use the meta_key for position
and list users in this order: "Managers", "Supervisors", "Foreman", "Coordinator", everyone else
The way I accomplished this query in the past, outside of Wordpress, was:
SELECT * FROM employees WHERE emp_department = 'Engineering' AND emp_employed = 'Y' AND emp_position LIKE '%Manager%' UNION SELECT * FROM employees WHERE emp_department = 'Engineering' AND emp_employed = 'Y' AND emp_position LIKE '%Sup%' UNION SELECT * FROM employees WHERE emp_department = 'Engineering' AND emp_employed = 'Y' AND emp_position LIKE '%Foreman%' UNION SELECT * FROM employees WHERE emp_department = 'Engineering' AND emp_employed = 'Y' AND emp_position LIKE '%Coordinator%' UNION SELECT * FROM employees WHERE emp_department = 'Engineering' AND emp_employed = 'Y' ORDER BY field(emp_position,'%MANAGER%', '%Sup%', '%Foreman%', '%Coordinator%', emp_id) ASC
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 server memory and crashes it. If I reduce the query to just checking for "managers" and "supervisors", it works....it only shows those positions and slow to load, 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?