How to query the list of user with a specific membership ?

Hi,

I'd like to show a list of member with specific membership.
How can i query it ?

Thanks for your help.

  • Kris

    Hi Imad,

    Hope you are doing good today.

    I am afraid we don't have members listing option in Membership 2 Pro. You would need to make some customizations to make this work.

    However, Membership 2 Pro plugin can be integrated with BuddyPress seamlessly. In BuddyPress you can go to Settings > BuddyPress > Pages tab and there select page where to show all your members. But adding additional filter would also require some advanced customization.

    In this case you can also use our Job Portal where experience WordPress developers from community work.
    https://premium.wpmudev.org/wordpress-development/

    Kind Regards,
    Kris

  • Kris

    Hi Imad,

    I asked my colleagues from WPMU support staff and they suggested me the perfect solution for you.

    In your theme find a functions.php file. Add this code:

    add_shortcode( 'show_members', 'show_members_cb' );
    function show_members_cb( $atts ) {
        $atts = shortcode_atts(
            array(
                'id' => 0
            ),
            $atts
        );
        if( $atts['id'] == 0 ) {
            return 'You need to provide a membership ID';
        }
        $args = array(
            'membership_id' => $atts['id'],
            'subscription_status' => 'active'
        );
        $members = MS_Model_Member::get_members( $args );
        $html = '<table cellpadding="5" cellspacing="5">';
            $html .= '<tr>';
                $html .= '<th>User ID</th>';
                $html .= '<th>Username</th>';
                $html .= '<th>Display Name</th>';
            $html .= '</tr>';
        foreach( $members as $member ) {
            $html .= '<tr>';
                $html .= '<td>' . $member->id . '</td>';
                $html .= '<td>' . $member->username . '</td>';
                $html .= '<td>' . $member->name . '</td>';
            $html .= '</tr>';
        }
        $html .= '</table>';
        return $html;
    }

    You can remove unnecessary columns if you need it. For example, you do not want the "User ID" column, so you are removing the code from the above code:

    $html .= '<th>User ID</th>';
    and
    $html .= '<td>' . $member->id . '</td>';
    See the screenshot:

    After that make a page with this shortcode:

    [show_members id="XX"]
    where instead XX just put your membership ID. To get your membership ID go to:
    Membership 2 -> Memberships and look at the end of table, there is a shortcode column, hover on "SHOW" button and you will see ID of selected Membership.

    I made a test on my site and it works great. Here is a proof:

    Let me know in response.

    Kind Regards,
    Kris

    • Ash

      Hi Lucy

      It might be possible, please try the following code:

      add_shortcode( 'show_members', 'show_members_cb' );
      function show_members_cb( $atts ) {
          $atts = shortcode_atts(
              array(
                  'name' => 0
              ),
              $atts
          );
          if( $atts['name'] == 0 ) {
              return 'You need to provide a membership name';
          }
      
      	global $wpdb;
          $prepare_guery = $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} where meta_key ='name' and meta_value = '%s'", $atts['name'] );
          $membership_id = $wpdb->get_var( $prepare_guery );
      
          $args = array(
              'membership_id' => $membership_id,
              'subscription_status' => 'active'
          );
          $members = MS_Model_Member::get_members( $args );
          $html = '<table cellpadding="5" cellspacing="5">';
              $html .= '<tr>';
                  $html .= '<th>User ID</th>';
                  $html .= '<th>Username</th>';
                  $html .= '<th>Display Name</th>';
              $html .= '</tr>';
          foreach( $members as $member ) {
              $html .= '<tr>';
                  $html .= '<td>' . $member->id . '</td>';
                  $html .= '<td>' . $member->username . '</td>';
                  $html .= '<td>' . $member->name . '</td>';
              $html .= '</tr>';
          }
          $html .= '</table>';
          return $html;
      }

      Now use the shrotcode as following:

      [show_members name="NAME GOES HERE"]

      Let us know if that works for you. Have a nice day!

      Cheers,
      Ash

  • Ash

    Hello Lucy

    Sorry, my bad! Please try the following code:

    add_shortcode( 'show_members', 'show_members_cb' );
    function show_members_cb( $atts ) {
        $atts = shortcode_atts(
            array(
                'name' => ''
            ),
            $atts
        );
        if( $atts['name'] == '' ) {
            return 'You need to provide a membership name';
        }
    
    	global $wpdb;
        $prepare_guery = $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} where meta_key ='name' and meta_value = '%s'", $atts['name'] );
        $membership_id = $wpdb->get_var( $prepare_guery );
    
        $args = array(
            'membership_id' => $membership_id,
            'subscription_status' => 'active'
        );
        $members = MS_Model_Member::get_members( $args );
        $html = '<table cellpadding="5" cellspacing="5">';
            $html .= '<tr>';
                $html .= '<th>User ID</th>';
                $html .= '<th>Username</th>';
                $html .= '<th>Display Name</th>';
            $html .= '</tr>';
        foreach( $members as $member ) {
            $html .= '<tr>';
                $html .= '<td>' . $member->id . '</td>';
                $html .= '<td>' . $member->username . '</td>';
                $html .= '<td>' . $member->name . '</td>';
            $html .= '</tr>';
        }
        $html .= '</table>';
        return $html;
    }

    Let us know if that works for you. Have a nice day!

    Cheers,
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.