[Membership 2 Pro] M2Pro Members Only In BP Member Directory

Hello,
I am trying to display only paid members from Membership 2 pro in the BuddyPress member directory. I found this nice write up which seemed promising, and it's exactly what I need to do, but I can't seem to get it working - http://tymescripts.com/show-paid-subscribers-wpmudev-membership-2-buddypress-members-directory/

  • Kasia Swiderska

    Hello Lou,

    I have tested that code on my site and it is also not working correctly. Most probably because it is from 2017 and since then both plugins had updates and code changed.
    Let me consult this with our Second Line Support developers if we can update that code so it will work with the latest BuddyPress and Membership 2 Pro and we will let you know about the result.

    kind regards,
    kasia

  • Konstantinos Xenos

    Hi Lou ,

    Could you please give it a try by adding the following code into your functions.php ( preferably your child themes ):

    if ( class_exists( 'MS_Model_Relationship' ) ) {
        function hide_members_bp_ms_find_paid_users() {
            $all_user_ids        = get_users( array( 'fields' => 'ID' ) );
            $ms_members_user_ids = '';
            $subscriptions       = '';
    
            foreach ( $all_user_ids as $user_id ) {
                $subscriptions = MS_Model_Relationship::get_subscriptions(
                    array(
                        'user_id' => $user_id,
                        'status'  => 'all',
                    )
                );
    
                foreach ( $subscriptions as $subscription ) {
                    if ( $subscription->get_status() == MS_Model_Relationship::STATUS_ACTIVE ||
                        $subscription->get_status() == MS_Model_Relationship::STATUS_TRIAL ) {
                        $ms_members_user_ids .= $user_id . ',';
                    }
                }
            }
    
            return rtrim( $ms_members_user_ids, ',' );
        }
    
        function hide_members_bp_ms_get_paid_users( $query, $object ) {
            if ( 'members' != $object ) {
                return $query;
            }
    
            $ms_members_user_ids = hide_members_bp_ms_find_paid_users();
    
            return $query .= '&type=alphabetical&include=' . $ms_members_user_ids;
        }
        add_filter( 'bp_ajax_querystring', 'hide_members_bp_ms_get_paid_users', 999, 2 );
    
        function hide_members_bp_ms_css() {
            echo '<style>#subnav, #members-all { display: none; }</style>';
        }
        add_action( 'wp_footer', 'hide_members_bp_ms_css', 999 );
    }

    Since each setup might have differences tell me if that worked for you or if it needs any extra tweaking.

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.