Skip to:
Content
Pages
Categories
Search
Top
Bottom

exclude_by_role function no longer working?

  • @2paulm

    Participant

    I am working with a clients site that uses exclude_by_role function to only show members who are subscribers in the member loop.

    code in /mytheme/buddypress/members/members-loop.php

    is as follows:

    <?php do_action( 'bp_before_members_loop' ); ?>
    
    <?php 
        $excluded_roles = array ('administrator', 'author', 'customer', 'editor', 'contributor', 'shop manager'); // this can be any roles you have set up
    
        if (bp_has_members( 'exclude=' . exclude_by_role($excluded_roles) . '&' . bp_ajax_querystring( 'members' ) ) ) : 
    
        //if ( bp_has_members( bp_ajax_querystring( 'members' ) ) ) : 
    ?>

    the function itself is in

    mytheme/functions.php

    and is as follows:

    function exclude_by_role($exclude_roles) {
    
    $memberArray = array();
    
    if (bp_has_members()) :
    while (bp_members()) : bp_the_member();
    $user = new WP_User( bp_get_member_user_id() );
    $user_role = $user->roles[0];
    foreach ($exclude_roles as $exclude_role) {
    if ($exclude_role==$user_role) {
    array_push($memberArray, $user->ID);
    break;
    }
    }
    endwhile;
    endif;
    
    $theExcludeString=implode(",",$memberArray);
    
    return $theExcludeString;
    
    }

    this code has worked to exclude certain rolls from the member results but does not seem to be doing anything anymore. all members are returned regardless of their rolls now.

    Please help if you can. it seems the code was also removed from github I am assuming because it is depreciated or something.

    any thoughts?

Viewing 3 replies - 1 through 3 (of 3 total)
  • @2paulm

    Participant

    Anyone? please help if you can

    @shanebp

    Moderator

    I don’t know why your function isn’t working, but since you only want subscribers, there are more efficient ways to approach the task.

    Here is one:
    Review this: https://codex.wordpress.org/Function_Reference/get_users

    Try something like:

    function subscribers_only() {
       $subscribers = get_users( array( 'role' => 'subscriber', 'fields' => array( 'ID' ) ) );
       $subscribers = implode(",",$subscribers);
       return $subscribers;
    }

    And

    $subscribers = subscribers_only();
    bp_has_members( bp_ajax_querystring( 'members' ) . '&include=' . $subscribers )

    @2paulm

    Participant

    shane thank you for your help I figured it out while I was waiting and used this function

    I added this to members-loop.php

    if ( bp_has_members( include_only_subscribers() ) ) :

    and added this to my themes function.php

    function include_only_subscribers() {
      
      global $wpdb;  
    
        $query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE user_id IN (SELECT user_id FROM wp_usermeta WHERE meta_value LIKE '%subscriber%')";
      
      $custom_ids = $wpdb->get_col( $query );
      
      if ( !empty( $custom_ids ) ) {
        // convert the array to a csv string
        $custom_ids_str = 'include=' . implode(",", $custom_ids);
        return $custom_ids_str;
      }
      else
       return '';
    }

    it seems to be working I just used some info from https://codex.buddypress.org/developer/loops-reference/the-members-loop/

    and modified to meet my needs.

    please let me know if you see any problem with my approach and thank you so much for your response and help with this

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘exclude_by_role function no longer working?’ is closed to new replies.
Skip to toolbar