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

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

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 排序自定义文章,并且其次按标题排序?

  1. add_action('elementor/query/jet-smart-filters', function($query) {
  2. $query->set('meta_key', 'fuel_type_id');
  3. $query->set('orderby', 'meta_value_num title'); // 设置第二个排序字段为标题
  4. $query->set('order', 'ASC');
  5. });
英文:

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?

  1. add_action( 'elementor/query/jet-smart-filters', function( $query ) {
  2. $query->set( 'meta_key', 'fuel_type_id' );
  3. $query->set( 'orderby', 'meta_value_num' );
  4. $query->set( 'order', 'ASC' );
  5. });

答案1

得分: 1

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

  1. add_action('elementor/query/jet-smart-filters', function ($query) {
  2. $query->set('meta_key', 'fuel_type_id');
  3. $query->set('orderby', array('meta_value_num', 'title'));
  4. $query->set('order', 'ASC');
  5. });

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.

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

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

  1. add_action( 'elementor/query/jet-smart-filters', function( $query ) {
  2. $query->set( 'meta_key', 'fuel_type_id' );
  3. $query->set( 'orderby', array( 'meta_value_num', 'title' ) );
  4. $query->set( 'order', 'ASC' );
  5. });

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.

  1. $query->set( 'orderby', array(
  2. 'meta_value_num' => 'ASC',
  3. 'title' => 'DESC'
  4. ) );

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:

确定