Skip Navigation

[Resolved] Orderby two custom fields in the same View

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to order a View by one custom field (primary) and another custom field (secondary).

Solution: Add the following code to your functions.php file to modify the Query:

add_filter( 'wpv_filter_query', 'order_by_two_custom_fields',199,3 );
function order_by_two_custom_fields( $query_args, $views_settings, $view_id) {
  $view_ids = array( 12345 );
  $args = array();
  if (in_array($view_id, $view_ids)){
    $args = array(
      'meta_query' => array(
        'relation' => 'AND',
        'function-clause' => array(
          'key' => 'wpcf-function',
          'compare' => 'EXISTS',
          'type' => 'NUMERIC'
        ),
        'begin-clause' => array(
          'key' => 'wpcf-begin_date',
          'compare' => 'EXISTS',
          'type' => 'NUMERIC'
        )
      ),
      'orderby' => array(
        'function-clause' => 'ASC',
        'begin-clause' => 'ASC'
      )
    );
  }
  return array_merge($query_args, $args);
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

100% of people find this useful.

This support ticket is created 6 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#780592

I have created a view displaying all the committee members, named 'committee_members'. The custom content type used by this view has two fields named 'begin_date' and 'function'

I am trying to:
ordered the members first by function (lower numerical value first) and if two members have the same function (so same number), displaying member with the lower begin_date before.

In the view i can only order by one custom field. If i include a shortcode to a page for displaying this view i can add orderby attribute, but this attribute seems to change nothing.

[wpv-view name="committee_members" orderby="wplc-function,wplc-begin_date" order="asc"]
#780891

This is tricky because it's not possible to do in wp-admin. You must use custom code to modify the Query. Add this code to your child theme's functions.php file:

add_filter( 'wpv_filter_query', 'order_by_two_custom_fields',199,3 );
function order_by_two_custom_fields( $query_args, $views_settings, $view_id) {
  $view_ids = array( 12345 );
  $args = array();
  if (in_array($view_id, $view_ids)){
    $args = array(
      'meta_query' => array(
        'relation' => 'AND',
        'function-clause' => array(
          'key' => 'wpcf-function',
          'compare' => 'EXISTS',
          'type' => 'NUMERIC'
        ),
        'begin-clause' => array(
          'key' => 'wpcf-begin_date',
          'compare' => 'EXISTS',
          'type' => 'NUMERIC'
        )
      ),
      'orderby' => array(
        'function-clause' => 'ASC',
        'begin-clause' => 'ASC'
      )
    );
  }
  return array_merge($query_args, $args);
}

Replace 12345 with the numeric ID of this View.

#821321

I just need to add :

  $args = array();

before the if otherwise it generates error with other views.

#830889

Ah, good catch. I have updated the code sample above.