Skip Navigation

[Resolved] Filtering by a number entered by the user between two stored field values

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

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 support ticket is created 5 years, 1 month 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 4 replies, has 2 voices.

Last updated by Simon Logan 5 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1387001

I've been looking at the documentation (such as https://toolset.com/forums/topic/filter-by-a-range-between-2-values-minimun-maximun/) but what I'm needing is slightly different so I'm not sure of the best approach to take.

I have a filter/search form which includes, amongst other fields, a text box of number type which the user will type a number into, ie 5000. This filters a custom post type of "financeproduct" which has "min loan amount" and "max loan amount" fields, so these might contain values of 750 and 10000 respectively. I'd like to be able to filter the listings based on the number the user has entered falling between the min and max amounts?

The difference, as I see it, is that I don' t want or need the actual min/max fields to appear on the search form, instead just that single "amount" field which is already there?

#1387319

Hi, there's no filter exactly like this in custom search Views unfortunately. You might be able to make it work with our Views API wpv_filter_query and some custom code: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

If you insert an input field in the filter area manually, you can access that field value in the filter callback from the $_GET superglobal. I would not expect this approach to work well with AJAX or with the "Only show available options for each input" feature active in the View.

Here's some example code for a filter like this:

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;
}

In this example, the input field name is 'range_number', and the max and min field slugs are maxslug and minslug, respectively.

#1387823

Thanks Christian, the info was exactly what I needed!

#1388653

Hi Christian

My apologies, I was so busy testing one thing I didn't notice another, so there are two small issues related to this which remain.

The main one is that I've noticed that, with my filter in place in the functions.php file, it now properly filters the products based on the range number being supplied falling between the min and max amount fields, however it ignores any other searches - so I have other fields in my search form alongside the range number one but if I leave the range number blank and search simply on one of these instead (ie "location") then no results are returned?

The other small thing is that with the input field I've manually entered (the range number one) when the form is submitted the URL param gets added in, as I'd expect, however if I then submit the form again (having adjusted my search params or not) then that range number URL param gets appended again so appears twice. That is to say, I go to hidden link then search on a loan amount of 25000, then page refreshes and the URL is appended (alongside the other params) with &loanamount=25000, however if I think adjust the loan amount to 35000 and search again the URL will be products&loanamount=25000&loanamount=35000. Is there a way to get this manual field updating in the URL params just as the other filters do?

#1388655

I'll start a new thread as I think I'm past the 12 hour limit!