简体   繁体   中英

Sort wp_query by multiple meta keys (numeric) in specific order

I need to sort a query by a specific order and I failed many times on finding the right solution, but nothing helped – so I'm asking for some hints what I'm doing wrong.

Short story: I have a wp_query showing bikes ordered by the cheapest price. It's working so far. But now I want to show some promotional bikes at first before the order by price starts. So the promotional bikes are showing first and then the "normal" loop.

Promotional bikes are tagged by a ACF field called "promotional_bikes".

This is my wp_query:

    $args = array(
        'post_type'         => 'bikes',
        'posts_per_page'    => -1,
        'facetwp'           => true,
        'post_status'       => 'publish',
        'orderby'           => 'meta_value_num',
        'order'             => 'ASC',
        'meta_key'          => 'baseprice_0_rate', // it's a repeater field in acf

        );

meta_key = baseprice_0_rate meta_value = 100 - 1000 // different values because of the prices

meta_key = promotional_bike meta_value = 1 // because true or false in acf

This was my last try:

    $args = array(
    'post_type'         => 'bike',
    'posts_per_page'    => -1,
    'facetwp'           => true,
    'post_status'       => 'publish',

    
    'meta_query' => array(

    'promo_bike' => array(
            'key' => 'promotional_bike',
            'compare' => 'EXISTS',
    ),
    'cheapest_bikes' => array(
            'key' => 'baseprice_0_rate',
            'compare' => 'EXISTS',
    ),
    ),

    'orderby' => array(
            'promotional_bike' => 'ASC',
            'cheapest_bikes' => 'meta_value_num',
    )

);

but I get 0 results and it doesnt work.

Can anybody give me a hint how to first show the promo bikes and then show the bikes (cheap to high) ?

Many thanks :-)

I came across this while trying to figure out how to orderby multiple numeric meta fields. Finally got it figured out and wanted to post some tips in case anyone else arrives here for the same reason.

Assuming the OP's query had returned results , here's how to sort by multiple numeric meta fields. The key is the type attribute when defining the meta_query clauses.

$args = [
  'post_type' => 'bike',
  'posts_per_page' => -1,
  'facetwp' => true,
  'post_status' => 'publish',
  
  'meta_query' => [
    'promo_bike' => [
      'key' => 'promotional_bike',
      'compare' => 'EXISTS',
      'type' => 'NUMERIC', // makes orderby sort numerically instead of alphabetically
    ],
    'cheapest_bikes' => [
      'key' => 'baseprice_0_rate',
      'compare' => 'EXISTS',
      'type' => 'NUMERIC',
    ],
  ],

  'orderby' => [
    'promo_bike' => 'DESC', // assuming you want promotional_bike=1 at the top
    'cheapest_bikes' => 'ASC',
  ],
]

Sources:
https://ideone.com/RUoyZs
https://wordpress.stackexchange.com/questions/246355/order-by-multiple-meta-key-and-meta-value

you don't need to code for that. simply you can short your product from Category>Count and then you can drag & drop... for better understanding - you can see my video tutorial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM