I have the following WP user query code:
//Get all users sorted by whatever
$sorted_users = new WP_User_Query(array(
'orderby' => 'meta_value',
'meta_key' => 'satisfaction_rating',
'order' => $order,
'role' => 'subscriber',
'meta_query' => array(
array(
'key' => 'satisfaction_rating',
'type' => 'NUMERIC'
)
)
));
//Set up our search fields
$search = new WP_User_Query(array(
'orderby' => $order_by,
'order' => $order,
'role' => 'subscriber',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'description',
'value' => $search,
'compare' => 'LIKE'
),
array(
'key' => 'full_name',
'value' => $search,
'compare' => 'LIKE'
)
)
));
//Get users filtered
$filtered_users = new WP_User_Query(array(
'role' => 'subscriber',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'hourly_rate',
'value' => array($filter_values['min_hourly_rate'], $filter_values['max_hourly_rate']),
'compare' => 'BETWEEN',
'type' => 'UNSIGNED'
),
array(
'key' => 'satisfaction_rating',
'value' => array($filter_values['min_satisfaction_rating'], $filter_values['max_satisfaction_rating']),
'compare' => 'BETWEEN',
'type' => 'UNSIGNED'
),
array(
'key' => 'country',
'value' => $filter_values['country'],
'compare' => ($filter_values['country'] == '' ? 'LIKE' : '=')
)
)
));
I then use a foreach loop to iterate over the sorted users array and drop any values that aren't present in one of the other queries. When scaled up, however, the execution time on this is very high due to the number of queries. I know that a direct SQL statement would be faster, but my SQL-fu is not very strong. I believe that I have to use joins to get the desired effect, as I need to get info both from meta and from the users table, but I don't know how I would begin to structure the statement. Can anybody give me a push in the right direction?
Aucun commentaire:
Enregistrer un commentaire