Skip Navigation

[Resolved] Orderby two custom fields in the same View, with one custom field is >= TODAY()

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

Problem:

Use custom codes to setup date field filters.

Solution:

This is a custom codes problem, see the solution provided by client:

https://toolset.com/forums/topic/orderby-two-custom-fields-in-the-same-view-with-one-custom-field-is-today/#post-2017809

Relevant Documentation:

https://toolset.com/documentation/user-guides/views/date-filters/

This support ticket is created 3 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.

Our next available supporter will start replying to tickets in about 5.68 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by Lara 3 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#2015211
2021-04-08 Order Custom Fields.PNG

Tell us what you are trying to do?

I try to order a view by two custom fields. It works fine so far. I just have a question regarding a small modification.
I use the code from the documentation (https://toolset.com/forums/topic/ordering-view-elements-by-two-custom-fields/)

add_filter( 'wpv_filter_query', 'sortieren_bewegunsplan_test',199,3 );
function sortieren_bewegunsplan_test( $query_args, $views_settings, $view_id) {
  
  $view_ids = array( 43705 );
  $args = array();
  if (in_array($view_id, $view_ids)){
    $args = array(
      'meta_query' => array(
        'relation' => 'AND',
        'day-clause' => array(
          'key' => 'wpcf-rask-von-tag',
          'compare' => 'EXISTS',
          'type' => 'NUMERIC'
        ),
        'time-clause' => array(
          'key' => 'wpcf-rask-von-uhrzeit-15-minuten',
          'compare' => 'EXISTS',
          'type' => 'NUMERIC'
        )
      ),
      'orderby' => array(
        'day-clause' => 'ASC',
        'time-clause' => 'ASC'
      )
    );
  }
  return array_merge($query_args, $args);
}

I need a little change in the day-clause. The wpcf-rask-von-tag should be a number greater than or equal TODAY() (see attached picture, where you can see the Query Filter of the View), instead of "compare with EXISTS"

Is there any documentation that you are following?
https://toolset.com/forums/topic/ordering-view-elements-by-two-custom-fields/

Is there a similar example that we can see?
no

What is the link to your site?
hidden link

#2015627

Hello,

How do you setup the "wpcf-rask-von-tag" field? is it a custom date field created with Toolset Types plugin?
If it is, you can follow our document to setup the date field filter:
https://toolset.com/documentation/user-guides/views/date-filters/

And you don't need to setup custom PHP codes for "wpcf-rask-von-tag" field.

#2016373

Hello Luo,

yes I did set up the date field filter according the following documentation: https://toolset.com/documentation/user-guides/views/date-filters/ (see attached picture of my first post).

However, the php overwrites the view's query filter for the "wpcf-rask-von-tag" field - and I need the php. Because without php, I can no longer order a view by two custom fields.

Yes, the "wpcf-rask-von-tag" is a custom date field created with Toolset Types plugin.

#2017809

I found a solution:

add_filter( 'wpv_filter_query', 'sortieren_bewegunsplan',199,3 );
function sortieren_bewegunsplan( $query_args, $views_settings, $view_id) {
  
  $view_ids = array( 43705 );
  $args = array();
  if (in_array($view_id, $view_ids)){
    
     $today = current_time('timestamp');
     $start_date = strtotime('0 day', $today);
    
    $args = array(
      'meta_query' => array(
        'relation' => 'AND',
        'day-clause' => array(
          'key' => 'wpcf-rask-von-tag',
          'value' => $start_date,
          'compare' => '>=',
          'type' => 'NUMERIC'
        ),
        'time-clause' => array(
          'key' => 'wpcf-rask-von-uhrzeit-15-minuten',
          'compare' => 'EXISTS',
          'type' => 'NUMERIC'
        )
      ),
      'orderby' => array(
        'day-clause' => 'ASC',
        'time-clause' => 'ASC'
      )
    );
  }
  return array_merge($query_args, $args);
}

My issue is resolved now. Thank you!