2

我正在使用带有 WooCommerce 的 Wordpress 4.9.4 版和WooCommerce 会员资格插件

如何获得特定会员计划 ($memberships) 的所有用户?

有钩子还是我应该使用原始查询?

在文档上,它说计划/会员资格是帖子类型,所以我尝试查询此表:

SELECT jsu.id, jsu.user_email, jsu.display_name, jsp.post_title, jsp.post_type FROM `wp_users` jsu
LEFT JOIN `wp_posts` jsp ON (jsu.id = jsp.post_author)
LEFT JOIN `wp_usermeta` jsum ON (jsu.id = jsum.user_id)
WHERE jsp.post_title IN ($memberships) AND post_type = 'wc_membership_plan'

但我没有得到想要的结果。

4

1 回答 1

4

我对您的 SQL 进行了一些更改,并在实用程序函数中设置了代码:

function get_active_members_for_membership($memberships){
    global $wpdb;

    // Getting all User IDs and data for a membership plan
    return $wpdb->get_results( "
        SELECT DISTINCT um.user_id, u.user_email, u.display_name, p2.post_title, p2.post_type
        FROM {$wpdb->prefix}posts AS p
        LEFT JOIN {$wpdb->prefix}posts AS p2 ON p2.ID = p.post_parent
        LEFT JOIN {$wpdb->prefix}users AS u ON u.id = p.post_author
        LEFT JOIN {$wpdb->prefix}usermeta AS um ON u.id = um.user_id
        WHERE p.post_type = 'wc_user_membership'
        AND p.post_status IN ('wcm-active')
        AND p2.post_type = 'wc_membership_plan'
        AND p2.post_title LIKE '$memberships'
    ");
}

此代码位于您的活动子主题(或主题)的 function.php 文件中。测试和工作。

用法:

// Replace 'My Membership' by your targeted membership name.
$users_data = get_active_members_for_membership('My Membership');

// Test raw output
echo '<pre>'; print_r( $users_data ); echo '</pre>';
于 2018-03-12T20:49:20.440 回答