我试图获得所有具有有效订阅的用户的列表,但对于我的生活似乎无法得到一些有用的东西。
如果我查看仪表板中的用户,我会看到所有活跃成员都有“Abonné”角色。
所以我设置了这样的短代码:
function custom_get_members( ){
$args1 = array(
'role' => 'Abonné',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$subscribers = get_users($args1);
$output = '<ul>';
foreach ($subscribers as $user) {
$output .= '<li>' . $user->display_name.'['.$user->user_email . ']</li>';
}
$output .= '</ul>';
return $output;
}
add_shortcode( 'annuaire', 'custom_get_members' );
但我总是得到0结果,即使我们目前有超过50个订阅者。
使用订阅和成员身份运行WooCommerce。
谢谢!
更新(为您的短代码添加了额外的自定义功能)
1)以下自定义函数生成一个非常轻的SQL查询将返回来自所有活动订阅者的用户ID数组:
function get_active_subscribers_ids(){
global $wpdb;
// Return an array of user Ids active subscribers
return $wpdb->get_col( "
SELECT DISTINCT pm.meta_value
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm
ON p.ID = pm.post_id
WHERE p.post_type = 'shop_subscription'
AND p.post_status = 'wc-active'
AND pm.meta_key = '_customer_user'
" );
}
代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。
2)对于你的短代码,我已经改变了这个函数来直接得到display_name
和user_email
:
function get_all_active_subscribers(){
global $wpdb;
return $wpdb->get_results( "
SELECT DISTINCT u.*
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm
ON p.ID = pm.post_id
JOIN {$wpdb->prefix}users as u
ON pm.meta_value = u.ID
WHERE p.post_type = 'shop_subscription'
AND p.post_status = 'wc-active'
AND pm.meta_key = '_customer_user'
" );
}
代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。
用于您的短代码:
function custom_get_members( ){
$output = '<ul>';
// Loop through active subscribers
foreach ( get_all_active_subscribers() as $user ) {
$output .= '<li>' . $user->display_name.'['.$user->user_email . ']</li>';
}
return $output . '</ul>';
}
add_shortcode( 'annuaire', 'custom_get_members' );
经过测试并使用更轻松的查询和代码。