Problem: I would like to create a front-end filter where the User can enter a number. Then I would like to filter the results such that one custom field is the minimum value and another custom field is the maximum value, and the User-entered number exists between that range.
Solution: There's nothing exactly like this in Views, so it will require a custom numeric input field and custom code using the Views API wpv_filter_query. An example is shown below:
add_filter('wpv_filter_query', 'between_custom_field_values', 99, 3); function between_custom_field_values($query_args, $view_settings, $view_id ) { $view_ids = array( 123, 456 ); // change this to a comma-separated list of the View ids you want to filter if( in_array( $view_id, $view_ids)) { $range_number = isset($_GET['range_number']) ? $_GET['range_number'] : null; if ($range_number !== null) { $query_args['meta_query'][] = array( 'relation' => 'AND', array( 'key' => 'wpcf-maxslug', 'value' => $range_number, 'compare' => '>=', 'type' => 'NUMERIC' ), array( 'key' => 'wpcf-minslug', 'value' => $range_number, 'compare' => '<=', 'type' => 'NUMERIC' ) ); } } return $query_args; }
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 4 replies, has 2 voices.
Last updated by 5 years, 1 month ago.
Assisted by: Christian Cox.