How can I pull all members from a certain subscription?

I'm trying to pull a list of members who are attached to a certain subscription. The website is a sort of member directory, but there are other members who shouldn't be shown in this directory.

I can't find any documentation on pulling members based on a membership type - or much developer-based documentation at all to be honest.

The code I have currently is as follows:

$args = array (
    array( 'role' => 'Subscriber' )
$user_query = new WP_User_Query( $args );
$members = $user_query->get_results();
if (!empty($members)): ?>
    <?php foreach ($members as $member): ?>
        // get all the user's data
        $member_info = get_userdata($member->ID);
        echo '<li>' . $member_info->first_name . ' ' . $member_info->last_name . '</li>';
    <?php endforeach; ?>
<?php else: ?>
    Sorry, there were no members matching your search.
<?php endif; ?>

I'm looking to expand this to only query users of the membership roles 'company' and 'member', and then to add some additional parameters based on custom fields attached.

Any help is massively appreciated.

  • Luís

    Hi Mike Griffiths ,

    Hope you're doing well today!

    Membership 2 Pro have some documentation inside of Membership 2 -> Help -> API Docs. If I understood well your question, you will need the "get_members()" function (it's inside the "MS_Model_Membership" section of the API documentation).

    Can you try something like:

    $args = array(
    	        'membership_id' => 242,
    	        'subscription_status' => 'active'
    	$members = MS_Model_Member::get_members( $args );
    	$html = '<ul>';
    	    foreach( $members as $member ) {
    	        $html .= '<li>' . $member->first_name . ' | ' . $member->last_name . '</li>';
    	    $html .= '</ul>';
    	    return $html;

    Note: You need to replace the "membership_id" to the membership desired.

    I hope this information has been helpful. If I can help you in this or other questions, please let me know!

    Cheers, Luís

  • Mike Griffiths

    Hi Luís,

    While that does work, I'm not sure it will allow me to query multiple membership IDs. From looking at the code within the MS_Model_Member class, it looks like intval() is run on the value of membership_id, so only a single int will work.

    Ideally I'd like to pass an array:

    $args = array(
        'membership_id' => array(31, 18),
        'subscription_status' => 'active'

    So, how can I achieve what I need? There's a potentially large set of members, and they will be paginated, so it needs to be done in one query, rather than multiple and concatenate the results.

  • Ash

    Hello Mike Griffiths

    There is no way to pass multiple membership IDs in the parameter. We could introduce a function but in the backend, it will do the same - I mean still it will call the same function multiple times. So you can do that using a loop, like following:

    function get_users_of_memberships( $membership_ids = array() )
            $users = array();
            foreach( $membership_ids as $membership_id )
                    $args = array(
                            'membership_id' => 242,
                            'subscription_status' => 'active'
                    $members = MS_Model_Member::get_members( $args );
                    $users = array_merge( $users, $members );
            return $users;
    $membership_ids = array( 101, 202, 303 );
    $users = get_users_of_memberships( $membership_ids );

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.