我有一个自定义meta\\u键,名为“date\\u of\\u birth”
我正试图根据该值创建一个每年的用户计数表。
我有这个代码可以按特定年份计算,也可以计算受试者的年龄:
$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;
我需要的是某种循环,让所有用户使用元键“date\\u of\\u birth”,然后每年循环创建以下图表:
YEAR TOTAL OVER18 UNDER18
2017 5 3 2
2016 2 1 1
ETC... so on .. so forth
但我需要表格是动态的,以便明年它将2018年的当前年份添加到开头并继续。。。
我原以为WP\\u查询可能会起作用,但我也找不出适合该查询的正确代码。
SO网友:GRRRRwordpressgrrr
因此,我通过执行一个常规的get\\u user()并将其放入一个for循环中,每年运行get\\u user来解决这个问题。
$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.
}