Ecommerce Shopify WordPress Discussion

How to display posts in order of ACF field values

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; } Run code snippetExpand snippet
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.