在PRE_GET_POST中按多个元键排序

时间:2016-05-03 作者:Aman

我需要多个orderby meta\\u查询,例如:

orderby(‘_property_price’ => ‘DESC’,’_property_featured’ => ‘DESC’)
这就是我正在做的

$query->set(\'meta_key\',array(\'_property_price\',\'_property_featured\'));
$query->set(\'orderby\',array(\'meta_value\'=>\'DESC\'));
在SQL中,应该是这样的

ORDER BY _property_price, _property_featured DESC
有什么解决办法吗?

1 个回复
SO网友:s t

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;
    } );