Set a 2nd orderby value for a query action? (为查询操作设置第二个排序值?)

huangapple go评论63阅读模式
英文:

Set a 2nd orderby value for a query action?

问题

I'm trying to order some custom posts by a meta_value, and then secondly order them by the title. How can I set a second orderby value in the below action?

在下面的操作中,如何设置第二个 orderby 值,以便按 meta_value 排序自定义文章,并且其次按标题排序?

add_action('elementor/query/jet-smart-filters', function($query) {

  $query->set('meta_key', 'fuel_type_id');
  $query->set('orderby', 'meta_value_num title'); // 设置第二个排序字段为标题
  $query->set('order', 'ASC');

});
英文:

I'm trying to order some custom posts by a meta_value, and then secondly order them by the title. How can I set a second orderby value in the below action?

add_action( 'elementor/query/jet-smart-filters', function( $query ) {
	
  $query->set( 'meta_key', 'fuel_type_id' );
  $query->set( 'orderby', 'meta_value_num' );
  $query->set( 'order', 'ASC' );

});

答案1

得分: 1

Use this code to set a second-order by value in your above action.

add_action('elementor/query/jet-smart-filters', function ($query) {

  $query->set('meta_key', 'fuel_type_id');
  $query->set('orderby', array('meta_value_num', 'title'));
  $query->set('order', 'ASC');

});

You can also set the order using ascending/descending for each order by value separately by passing in the array. For that, you can use the below example.

$query->set('orderby', array(
    'meta_value_num' => 'ASC',
    'title' => 'DESC'
));
英文:

Use this code the set a second order by value in your above action.

add_action( 'elementor/query/jet-smart-filters', function( $query ) {
    
  $query->set( 'meta_key', 'fuel_type_id' );
  $query->set( 'orderby', array( 'meta_value_num', 'title' ) );
  $query->set( 'order', 'ASC' );

});

You can also set the order using ascending/descending for each order by value separately by passing in the array. for that, you can show the below example.

$query->set( 'orderby', array(
    'meta_value_num' => 'ASC',
    'title' => 'DESC'
) );

huangapple
  • 本文由 发表于 2023年4月13日 20:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定