I have a list of dogs up for adoption. I would like to filter them by age groups progmatically.
Ie:
User is given the choices:
Puppy
Young Adult
Adult
Senior
But the search form uses the DOB (stored in an ACF field as MM Y) to filter the results into the 4 gorups
I already display the text age using this IF
$now = new DateTime();
$dob = new DateTime($data);
$difference = $now->diff($dob);
$age = $difference->m + 12*$difference->y;
if ($age <= 12) {
$agegroup = "Age: Puppy/Kitten";
} elseif ($age <= 36) {
$agegroup = "Age: Young Adult";
} elseif ($age <= 72) {
$agegroup = "Age: Adult";
} elseif ($age > 72) {
$agegroup = "Age: Senior";
}
Is there a nice way to do this? I would imagine running this for every search query could be quite resource intensive, so if there was a way to cache it and refresh once a week?
Previously the age group was a field, but this meant older dogs could still be classified as puppies as they weren’t ever updated.