3

I want multiple orderby meta_query eg:

orderby(‘_property_price’ => ‘DESC’,’_property_featured’ => ‘DESC’)

This is what I am doing

$query->set('meta_key',array('_property_price','_property_featured'));
$query->set('orderby',array('meta_value'=>'DESC'));

In SQL, it should be like

ORDER BY _property_price, _property_featured DESC

Any Solution to this ?

1 Answer 1

6

Maybe a bit late …

    add_action( 'pre_get_posts', function($query) {
        if ( !is_admin() && $query->is_main_query() && is_post_type_archive( '[your_post_type]' ) ) {
            $query->set('meta_query', array(
                '_property_price' => array(
                    'key' => '_property_price',
                ),
                '_property_featured' => array(
                    'key' => '_property_featured',
                )                    
            ));
            $query->set('orderby',array(
                '_property_price' => 'DESC', 
                '_property_featured' => 'DESC'
            ));     

        }

        return $query;
    } ); 
1
  • 2
    Generally it would be better to use a named function rather than an anonymous one, so that other code can unhook this filter. Aug 10, 2017 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.