我们在写会员展示插件的时候,可能会涉及到不同期限的会员,那么如何可以分别获取《月卡、季卡、年卡、永久》会员的数量呢?
那么今天就给大家带来这个问题的解决方法!其实也是非常简单代码使用了WP_User_Query 类进行数据库查询操作。
具体代码:
//月卡、季卡、年卡、永久会员的数量查询
function get_expiring_vip_user_count($type) {
$current_date = date("Y-m-d");
$expiring_count = 0;
switch ($type) {
case 'monthly':
$one_month_later = strtotime("+1 month");
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'vip_exp_date',
'value' => array($current_date, date("Y-m-d", $one_month_later)),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
);
$args = array(
'meta_query' => $meta_query,
'count_total' => true,
);
$user_query = new WP_User_Query($args);
$expiring_count = $user_query->get_total();
break;
case 'quarterly':
$two_months_later = strtotime("+2 months");
$three_months_later = strtotime("+3 months");
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'vip_exp_date',
'value' => array(date("Y-m-d", $two_months_later), date("Y-m-d", $three_months_later)),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
);
$args = array(
'meta_query' => $meta_query,
'count_total' => true,
);
$user_query = new WP_User_Query($args);
$expiring_count = $user_query->get_total();
break;
case 'yearly':
$eleven_months_later = strtotime("+11 months");
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'vip_exp_date',
'value' => date("Y-m-d", $eleven_months_later),
'compare' => '>',
'type' => 'DATE',
),
);
$args = array(
'meta_query' => $meta_query,
'count_total' => true,
);
$user_query = new WP_User_Query($args);
$expiring_count = $user_query->get_total();
break;
case 'permanent':
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'vip_exp_date',
'value' => 'Permanent',
'compare' => '=',
),
);
$args = array(
'meta_query' => $meta_query,
'count_total' => true,
);
$user_query = new WP_User_Query($args);
$expiring_count = $user_query->get_total();
break;
}
return $expiring_count;
}
在需要调用的地方输出
-
$monthly_count = get_expiring_vip_user_count(‘monthly’);
-
$quarterly_count = get_expiring_vip_user_count(‘quarterly’);
-
$yearly_count = get_expiring_vip_user_count(‘yearly’);
-
$permanent_count = get_expiring_vip_user_count(‘permanent’);
-
echo “月卡会员数量:” . $monthly_count . “<br>”;
-
echo “季卡会员数量:” . $quarterly_count . “<br>”;
-
echo “年卡会员数量:” . $yearly_count . “<br>”;
-
echo “永久会员数量:” . $permanent_count . “<br>”;
举例:
<?php $monthly_count = get_expiring_vip_user_count('monthly');
echo "月卡会员数量:" . $monthly_count . "<br>";
?>
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END