0

I have a custom meta_key called "date_of_birth"

i am trying to create a table of user counts for each year based on that value.

I have this code to count by specific year and also to calculate the age for over under test:

    $usercount = get_users(array('meta_key' => 'date_of_birth', 'meta_value' => 
    '1991', 'count_total' => true, 'meta_compare' => 'LIKE'));

    //count how many were born in 1991
    $countfor1991= count($users);

    //get age for over under test
    $birthday = $user->date_of_birth; 
    $age = date_diff(date_create($birthday), date_create('today'))->y;

what i need is some kind of loop that gets all the users with metakey "date_of_birth" and then cycles through each year to create the following chart:

YEAR TOTAL OVER18 UNDER18
2017   5     3      2
2016   2     1      1
ETC... so on .. so forth

But i need the table to be dynamic so that next year it will add the current year of 2018 to the beginning and continue on...

I thought maybe a WP_Query would work, but i can't figure out the right code for that one either.

1 Answer 1

0

So, I solved this by doing a regular get_user() and putting it inside a for loop of years and ran the get_user on each year.

$currentYear = date('Y');
$years = range($currentYear, 1900);

foreach($years as $year){

    $TotalArray = get_users(array('meta_key' => 'date_of_birth', 'meta_value' => $year, 'count_total' => true, 'meta_compare' => 'LIKE', 'role__in' => 'members'));
    $thisYearsTotal = count($TotalArray);

//echo some <tr> and <td> html for formatting.  

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.