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:

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.

Read more here: List users by Year of Birth using a foreach loop to dynamically populate years and data


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: