Skip Navigation

[Gelöst] Filter a custom field with a range of numbers

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:

The issue here is that the user wanted to filter their posts by a range of numbers using one filter.

Example
1970-1980
1980-1990
1990-2000

So we will have a filter that allows for the filtering of dates between those options.

Solution:
So you will need to add the filter for the custom field to your view but instead of setting that filter to load from the database we are going to add our custom values. So your filter should be generated like the following below.

Example


<div class="form-group">
    <label>[wpml-string context="wpv-views"]Number of pages[/wpml-string]</label>
    [wpv-control-postmeta field="wpcf-active-year" type="select" source="custom" url_param="wpv-wpcf-active-year" values="1970-1980,1980-1990,1990-2000" display_values="1970-1980, 1980-1990,1990-2000"]
</div>

If your custom field is already stored like this then you won't need to go through all this above, however i'm assuming that the values are stored as individual years.

Ofcourse for this to work you will need to use the hook below. Granted this will only work if you are not using the AJAX reload.

add_filter('wpv_filter_query', 'search_between_numeric_func', 10, 3);
function search_between_numeric_func($query, $views_setting,$view_id) {
    if($view_id == '614' && isset($_GET['wpv-wpcf-active-year'])) {
      $val = $_GET['wpv-wpcf-active-year'];
      
       $query['meta_query']= array(
								array(
									'key'     => 'wpcf-active-year',
									'value'   => explode("-",$val),
                                    'type'    => 'numeric',
									'compare' => 'BETWEEN',
								),
							);
    }
          return $query;

}

Now everywhere you see me have wpcf-active-year you will need to replace it with the custom field slug, using the wpcf- as a prefix to the slug.

From the filter fields and the hook you can see how the items line up. The wpv-wpcf-active-year is a direct reference to the url parameter for the filter shortcode.

Also you need to replace the 614 with your correct view ID.

You can add this custom hook to your Toolset custom code in Toolset->Settings->Custom code.

This support ticket is created vor 4 Jahre, 11 Monate. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 2 Antworten, has 2 Stimmen.

Last updated by Cesar vor 4 Jahre, 11 Monate.

Assisted by: Shane.

Author
Artikel
#1229564

Hi,

I'm trying to filter my Custom Post Field under Views and I need to filter the value (number) in a range of 10

To put it clear I have numbers from 1970 to 2020 and I need to show results for several ranges
1970-1980
1980-1990
1990-2000
...
2010-2020

I've follow the following post:
https://toolset.com/forums/topic/seting-a-filter-by-price-range/

But it seems to be not fully resolved

The solution I need is just the one provided in this post:
https://toolset.com/forums/topic/date-range-filter-custom-field-checkboxes/

What I miss is the correct query in the function to check for a number between the values I need

I would really appreciate some help

Thanks

#1229631

Shane
Supporter

Languages: Englisch (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Cesar,

As Minesh said in the ticket this will require some custom coding to achieve.

So you will need to add the filter for the custom field to your view but instead of setting that filter to load from the database we are going to add our custom values. So your filter should be generated like the following below.

Example


<div class="form-group">
    <label>[wpml-string context="wpv-views"]Number of pages[/wpml-string]</label>
    [wpv-control-postmeta field="wpcf-active-year" type="select" source="custom" url_param="wpv-wpcf-active-year" values="1970-1980,1980-1990,1990-2000" display_values="1970-1980, 1980-1990,1990-2000"]
</div>

If your custom field is already stored like this then you won't need to go through all this above, however i'm assuming that the values are stored as individual years.

Ofcourse for this to work you will need to use the hook below. Granted this will only work if you are not using the AJAX reload.

add_filter('wpv_filter_query', 'search_between_numeric_func', 10, 3);
function search_between_numeric_func($query, $views_setting,$view_id) {
    if($view_id == '614' && isset($_GET['wpv-wpcf-active-year'])) {
      $val = $_GET['wpv-wpcf-active-year'];
      
       $query['meta_query']= array(
								array(
									'key'     => 'wpcf-active-year',
									'value'   => explode("-",$val),
                                    'type'    => 'numeric',
									'compare' => 'BETWEEN',
								),
							);
    }
          return $query;

}

Now everywhere you see me have wpcf-active-year you will need to replace it with the custom field slug, using the wpcf- as a prefix to the slug.

From the filter fields and the hook you can see how the items line up. The wpv-wpcf-active-year is a direct reference to the url parameter for the filter shortcode.

Also you need to replace the 614 with your correct view ID.

You can add this custom hook to your Toolset custom code in Toolset->Settings->Custom code.

Please let me know if this helps.

Thanks,
Shane

#1230176

Thank you!
This work like a charm and was super easy.
You rock!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.