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