Skip to:
Content
Pages
Categories
Search
Top
Bottom

Member Loop only MemberPress members


  • rsmithgs
    Participant

    @rsmithgs

    Hello,

    I know there are quite a few topics already on excluding people from the Members Loop, but this one is a bit different.
    I’m trying to exclude people who do not have MemberPress Memberships. I’ve seen several based on user role and extending the directory via a custom query (https://buddypress.org/support/topic/extend-bp_members-custom-query/), but nothing related to MemberPress. I’m really hoping I will not need to do as this custom query guy did it and ping my database for every member to see if a membership exists.

    Here is how I’m attempting to do it without luck (within members-loop.php which is located in wp-content/themes/THEME/buddypress/members):

    <?php while ( bp_members() ) : bp_the_member(); ?>
            <?php if(user_can(bp_get_member_user_id(), 'mepr-active', 'memberships: 36, 37, 38, 189, 198, 325, 1141, 1142, 1143, 1127, 1130, 1138')): ?>
Viewing 5 replies - 1 through 5 (of 5 total)

  • shanebp
    Moderator

    @shanebp

    You can pass a string of ids to the exclude parameter of bp_has_members.

    How you gather the ids of the members you want to exclude is up to you.
    Perhaps MemberPress has a function for getting all the user ids with a membership.
    If so, then use the include parameter instead.

    <?php
    
       // gather the ids of the members you want to exclude.
    
       // for example, say you end up with an array of user_ids:
    
       $exclude_these_users = array( 3, 7 );
    
       $exclude_string = '&exclude=' . implode (", ", $exclude_these_users);
    
    ?>
    
    <?php if ( bp_has_members( bp_ajax_querystring( 'members' ) . $exclude_string ) ) : ?>

    rsmithgs
    Participant

    @rsmithgs

    Hey @shanebp thank you so much for your fast response!

    Unfortunately, MemberPress support didn’t know of a function that gets user IDs with specific memberships so they handed me this SQL query:

    SELECT DISTINCT user_id FROM wp_mepr_transactions WHERE status IN('confirmed','complete') AND (expires_at >= NOW() OR expires_at = '0000-00-00 00:00:00') AND product_id = 123;

    Where I would need to change my Product ID to match the Membership ID. I’m assuming I would just add this SQL query to your code bit:

    <?php //get ids of members I want to include by grabbing members from db
    $memberPressMembers = $wpdb->get_results("SELECT DISTINCT user_id FROM wp_mepr_transactions WHERE status IN('confirmed','complete') AND (expires_at >= NOW() OR expires_at = '0000-00-00 00:00:00') AND product_id = MEMBERSHIPID");
    
    $include_string = '&include=' . implode(", ", $memberPressMembers); ?>
    
    <?php if ( bp_has_members( bp_ajax_querystring( 'members' ) . $include_string ) ) : ?>
    

    This would only call my database 1 time to grab all members who have those specific IDs.

    Is that correct? Am I missing anything? When I attempted this I kept getting a 500 error on my admin_ajax.


    rsmithgs
    Participant

    @rsmithgs

    Hello,

    So I found that if I change my $memberPressMembers to:

    $memberPressMembers = $wpdb->get_results("SELECT DISTINCT user_id FROM wp_mepr_transactions WHERE status IN('confirmed','complete') AND (expires_at >= NOW() OR expires_at = '0000-00-00 00:00:00') AND product_id = MEMBERSHIPID", ARRAY_N);

    Then I actually can see my list of IDs when I do this:

    echo '<script>console.log(' . json_encode($memberPressMembers, JSON_HEX_TAG) . ');</script>';

    The only issue is, if I do this but add the $include_string to the bp_has_members line it displays no members.
    I believe this is because ARRAY_N is placing all of my items into arrays. Looking at the console log I see that the arrays split at 100 records (0-99, 100-199, etc.).

    I’ve also tried then taking my memberPressMembers variable and adding each into an array ($include_these_users) like so:

    foreach($memberPressMember as $member){
     array_push($include_these_users, $member);
    }

    with no luck.
    Could you help me in forming my variable so it will work in the include_string?

    Thank you! I should probably note, I’m attempting all of this code within members-loop.php. I’m assuming this is still the correct spot to do this as the bp_has_members(bp_ajax_querystring(‘members’)) is part of the loop.


    shanebp
    Moderator

    @shanebp

    Try removing the ARRAY_N.

    Instead of $wpdb->get_results try $wpdb->get_col which should return a one dimensional array of ids.

    https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column


    rsmithgs
    Participant

    @rsmithgs

    Thank you so much! In case you couldn’t tell I’m not that familiar with accessing the database.

    This completely worked, just in case anyone needs this for the future here is what I used:

    <?php //get ids of members I want to include by grabbing members from db
    $memberPressMembers = $wpdb->get_col("SELECT DISTINCT user_id FROM wp_mepr_transactions WHERE status IN('confirmed','complete') AND (expires_at >= NOW() OR expires_at = '0000-00-00 00:00:00')");
    
    $include_string = '&include=' . implode(", ", $memberPressMembers); 
    //print out array of IDs to console to find how many people we are getting
    //echo '<script>console.log(' . json_encode($memberPressMembers, JSON_HEX_TAG) . ');</script>';
    ?>
    
    <?php if ( bp_has_members( bp_ajax_querystring( 'members' ). $include_string ) ) : ?>
    

    I didn’t actually need product id, because I want anyone who has an active membership to appear here, but be careful about a lot of “OR” statements as this could lead to grabbing people who also have a pending transaction as well (displaying people with pending but not complete transactions). If you want specific memberships I would probably add

    AND product_id IN (MEMBERSHIPIDs)

    Thanks again for all of your help @shanebp!

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar