A
A
Alexey Voropaev2017-04-24 01:59:22
SQL
Alexey Voropaev, 2017-04-24 01:59:22

How to make complex orderby in WP_QUERY?

I have 2 fields: min_variation_price , max_variation_price
In wp_query I write the following parameters:
'orderby' => 'min_variation_price max_variation_price',
'order' => 'ASC'
Everything works, but there is 1 problem, one of the fields can be empty and they fall to the begining.
I need to make the empty field go to the end.
Tell me how to implement this in wp_query
When I wrote a manual query, I did this:

CASE WHEN pmeta.meta_value = '' THEN '99999' END, pmeta.meta_value ASC

But I need to do it through wp_query

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Voropaev, 2017-04-24
@boomrap

Solved the problem:

add_filter( 'posts_orderby', array( $this, 'query_orderby' ) );
$gmw_query = new WP_Query( $this->form['query_args'] );
remove_filter( 'posts_orderby', array( $this, 'query_orderby' ) );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question