Problem: I would like to include a custom search filter that allows users to filter by past events or future events. This is the only filter I would like to include, and I want the results to update with AJAX.
Solution:
1. Add a custom select field to your View's Filter Controls editor. If this section is not visible, you must enable it in the Screen Options tab at the top of the admin screen. Insert the following code:
<select name="custom-end-filter" id="custom-end-filter" class="js-wpv-filter-trigger form-control"> <option value="">All</option> <option value="1">Upcoming</option> <option value="2">Past</option> </select>
2. Add the following custom code to functions.php:
add_filter( 'wpv_filter_query', 'special_end_filter', 99, 3 ); function special_end_filter( $query_args, $view_settings, $view_id ) { $the_end = isset($_GET['custom-end-filter']) ? $_GET['custom-end-filter'] : ''; if( $view_id == 12345 && $the_end ) { $compare = $the_end == '1' ? '>' : '<'; $args = array( 'relation' => 'AND', array( 'key' => 'wpcf-the-end', 'value' => strtotime('23:59:59'), 'compare' => $compare, 'type' => 'numeric' ) ); // add these arguments to your meta query $query_args['meta_query'] = isset($query_args['meta_query']) ? $query_args['meta_query'] : []; $query_args['meta_query'][] = $args; } return $query_args; }
Replace 12345 with your View's numeric ID, and replace 'wpcf-the-end' with the slug of your end date field. Use the 'wpcf-' prefix here.
3. Add a standard filter to this View's Filter Controls and hide it with CSS. Example Category filter:
<div class="form-group" style="display:none;"> <label>[wpml-string context="wpv-views"]Categories[/wpml-string]</label> [wpv-control-post-taxonomy taxonomy="category" type="select" url_param="wpv-category"] </div>
Relevant Documentation:https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
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 6 replies, has 2 voices.
Last updated by 7 years, 2 months ago.
Assisted by: Christian Cox.