0
votes

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 :-)

1
Do you actually have posts where promotional_bike and baseprice_0_rate both exist? Because that is what your current meta query is demanding. - 04FS

1 Answers

-1
votes

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.