<?php
// Index birthday dates in a custom date field (stored as YYYY-MM-DD) as age.
// This facet can then filter by age range, for example in a Range List facet type:
// https://facetwp.com/help-center/facets/facet-types/range-list/
// E.g.
// under 50
// 50-60
// 60-70
// 70-80
// 80-90
// 90+
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'my_facet_name' == $params['facet_name'] ) { // change "my_facet_name" to the name of your date-based facet
$raw_value = $params['facet_value']; // date needs to be in YYYY-MM-DD format
$birthdate = new DateTime($raw_value);
$now = new DateTime('now');
$interval = $birthdate->diff($now);
$age = $interval->y; // age in years
$params['facet_value'] = $age;
$params['facet_display_value'] = $age;
}
return $params;
}, 10, 2 );