Skip Navigation

[Resolved] Searching by Age range with multiple values

This support ticket is created 6 years, 10 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: Asia/Hong_Kong (GMT+08:00)

This topic contains 1 reply, has 2 voices.

Last updated by Luo Yang 6 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#602813

I'd like to be able to search a set of resources (custom posts) by age range. The complication is that each resource has multiple values for age - an age range. For instance, resource 1 is for people from ages 1 - 5, but resource 2 is for people ages 5-12.

I'd like to show a custom search for resources by age range, for example if you search for resources for people aged 4-6 you'd be presented with both resource 1 and resource 2.

I can get this to work by using a number field (that allows multiple values) on the custom post type. The problem is the data entry is a little bulky. For each resource you'd have to enter EVERY age that is covered, so for a resource that' an option for anyone under the age of 18, you'd have to enter 19 values.

Can you help me figure out how I might set a range on a custom post type that can then be used as part of a custom search?

#602887
wpv-age.JPG
birthday.JPG

Dear Rachel,

I suggest you setup a custom date field to store people's birthday, then use Views filter hook wpv_filter_query to apply the age search, here are the detail steps:
1) Create a custom date field "birthday" in your custom post type, see screenshot: birthday.JPG
then setup the value of field "birthday" for each people

2) Create a view list posts of your custom post type, in section "Filter Editor", add a select dropdown menu "Age range"

<div class="form-group">
	<label>[wpml-string context="wpv-views"]Age range[/wpml-string]</label>
	[wpv-control-postmeta field="wpcf-birthday" type="select" url_param="wpv-age" source="custom" values=",0~5,5~15,15~25" display_values="All,0~5,5~15,15~25"]
</div>

You can add more options in it

3) in above view, section "Query Filter", add a custom field filter:
Select items with field:
birthday is a UNSIGNED between 0, URL_PARAM(wpv-age)
see screenshot wpv-age.JPG

4) Add below codes into your theme/functions.php

add_filter( 'wpv_filter_query', 'age_range_func', 99, 3 );
function age_range_func( $query_args, $view_settings, $view_id ) {
    if ( $view_id == 123 && isset($query_args['meta_query'])) {
		$time = current_time('timestamp');
		$birthday_range = array(0, $time);
		if(isset($_GET['wpv-age']) && $_GET['wpv-age'] != ''){
			$wpv_age = explode('~', $_GET['wpv-age']);
			if(isset($wpv_age[0]) && isset($wpv_age[1])){
				$birthday_range = array(
					strtotime('-' . $wpv_age[1] . ' year', $time), 
					strtotime('-' . $wpv_age[0] . ' year', $time)
				);
			}
		}
		
		foreach($query_args['meta_query'] as $k => $v){
			if(isset($v['key']) && $v['key'] == 'wpcf-birthday'){
				$v = array(
					'key' => 'wpcf-birthday',
					'type' => 'UNSIGNED',
					'value' => $birthday_range,
					'compare' => 'BETWEEN',
				);
				$query_args['meta_query'][$k] = $v;
				break;
			}
		}
    }
    return $query_args;
}

Please replace 123 with your view's ID of step 2)

More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query