Skip Navigation

[Resolved] Display all intermediary data on site in one View

This support ticket is created 3 years, 9 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
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: Africa/Casablanca (GMT+01:00)

This topic contains 18 replies, has 3 voices.

Last updated by Jamal 3 years, 9 months ago.

Assisted by: Jamal.

Author
Posts
#2181503

Do I need to put this in the JS editor of the View?

add_filter( 'wpv_filter_query', 'my_callback_function', 99, 3 );

$query_args['meta_query']['relation'] = 'OR';
}

#2181805

No. This is PHP code that is meant to run on the server. JS is meant to be run in the client browser.
The PHP code can be added on the theme's functions.php file, or on a plugin, or on Toolset->Settings->Custom code. Check these articles:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

You are somehow right, $query_args['meta_query']['relation'] needs to be 'OR' instead of 'AND'.

This can be done in many ways. However, regarding this view's case, we can do it this way:
- Have a search filter on Booking From. The search filter will have a from and until date inputs, and will generate a query filter for the view(booking-from between url paramteres "booking-from" and "booking-until").
- Have a custom code that will create the same query filter for booking-until.

I come up with the following snippet. I added it in Toolset custom code section hidden link
This custom code will change the meta_query relation from "AND" to "OR" and will recreate the same filter for wpcf-booking-until

add_filter( 'wpv_filter_query', 'my_search_venues_hook', 99, 3 );

function my_search_venues_hook( $query_args, $view_settings, $view_id){
  // $meta_query = $query_args['meta_query'];
  $meta_query = array();
  foreach( $query_args['meta_query'] as $key => $value ){
    if ( is_string( $key ) && $key == 'relation' ){ // This would be relation=AND
      $meta_query['relation'] = 'OR';
    }
    // Here we must mirror the condition on the other field wpcf-booking-until
    // If booking-from  is provided, mirror the condition for booking-until
    // If booking-until is provided, mirror the condition for booking-from
    if ( is_array( $value ) ){  // This would be the condition on the custom field(wpcf-booking-from)
      // Let's build the arguments for wpcf-booking-until
      $arr = $value;
      $arr['key'] = 'wpcf-booking-until'; 
      
      $meta_query[] = $value; // Add the filter for wpcf-booking-from
      $meta_query[] = $arr;   // Add a similar filter for wpcf-booking-until
    }
  }
  
  // If we have built any arguments, let's use them. We won't build arguments if 'booking-from' or 'booking-until' are not provided
  if ( sizeof( $meta_query ) > 0 )
    $query_args['meta_query'] = $meta_query;
  
  return $query_args;
}

Please check again from your side and let me know if I missed something.

#2182247

Jamal, thank you so, so much. 🙂 May I ask one last thing in relation to this View?

If you look at the search page, you will notice some of the shows have already passed. I want to hide all posts (or remove from search results) those that are appearing after their Booking Until date so I can only search current Shows

eg booking until the 6th Sept, after this date the result disappears because it is no longer available

I know I can do this using the following: [wpv-conditional if="( $(wpcf-booking-until) gte 'TODAY()' )"][/wpv-conditional]

But I have no idea where to put it in the View - whatever I have tried has made the whole thing disappear.

#2184489

Well, you should use a query filter instead of conditional shortcodes. Because the query filter will affect the database query. This way, the old posts won't be pulled from the database at all.

Go to the query filter section of the view, and add a filter on the booking until field, the posts should have a booking until that is higher than today.

Let me know if you need further assistance with it.