I have posts sorted by different parameters. Each post has an ACF select field top_rated. There is also sorting by Top Rated parameter – this is a separate parameter in the admin panel for each post. The ACF top_rated field can have 3 values: Number_1
Number_2
Number_3
I need to make sure that if the sorting is by Top Rated, then the first three posts are displayed those with the ACF value of the field top_rated Number_1
Number_2
Number_3 and only then all the rest. Now my code displays only the post with Number_1, but I need the first three posts with Number_1
Number_2
Number_3 and then all the rest. How to do it?
static function load_brokers_by_category($array_filter)
{
$tax_query = [];
if (!empty($array_filter)) {
$tax_query = [
'relation' => 'AND'
];
$all_taxonomy = get_object_taxonomies(['brokers']);
foreach ($array_filter as $taxonomy => $tax_item) {
if (in_array($taxonomy, $all_taxonomy)) {
if (is_array($tax_item)) {
foreach ($tax_item as $t_i) {
if (!empty($t_i)) {
$t_a[] = [
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => explode(',', $t_i)
];
}
}
if (!empty($t_a)) {
$tax_query[] = array_merge(['relation' => 'OR'], $t_a);
}
} else {
if (!empty($tax_item)) {
$tax_query[] = [
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => explode(',', $tax_item)
];
}
}
}
}
}
$sort_by = isset($_GET['sort_by']) ? sanitize_text_field($_GET['sort_by']) : (isset($_POST['sort_by']) ? sanitize_text_field($_POST['sort_by']) : 'post_date');
$wp_query_array = [
'posts_per_page' => -1,
'post_type' => 'brokers',
'post_status' => 'publish',
'tax_query' => $tax_query,
'order' => ($sort_by === 'post_date') || ($sort_by === 'top_rated') ? 'DESC' : (($sort_by === 'title') || ($sort_by === 'low_rated') ? 'ASC' : 'meta_value_num'),
'orderby' => ($sort_by === 'post_date') ? 'post_date' : (($sort_by === 'title') ? 'title' : 'meta_value_num'),
'meta_key' => 'rating'
];
if ($sort_by === 'top_rated') {
$wp_query_array['meta_query'] = [
'relation' => 'AND',
[
'key' => 'top_rated',
'value' => 'Number_1',
'compare' => '=',
],
];
}
$query_brokers = new WP_Query($wp_query_array);
return $query_brokers;
}