Skip to:
Content
Pages
Categories
Search
Top
Bottom

SOLVED: Limit Members Loop by Profile Fields

  • After long hours trying to fix this issue, I present to all who care, a simple way to only include only members with certain profile field values in a loop. I tried assigning roles to certain users through custom profile fields, and then pull only those certain members on specific listing pages.

    What I tried first was including an if statement within the loop and only showing the members info of those assigned the specific role. It worked – to a point. The problem came in with pagination where the loop was still recognizing all members that were queried, and creating pages to list them even though those pages were empty since these members did not meet the if condition and therefore were not displayed.

    So, I needed a different approach which I present to you here. Place this in your functions.php and you’re good to go. It runs a quick query on all members and returns a comma separated list of member id’s that fit the criteria to give you a list of id’s to include. See example below.

    `
    /* Include only members that have a specific profile value in a specific profile field in a loop.
    * $theMetaValue = Value of field to search for
    * $theMetaFielt = Field name to search for Meta Value
    * EXAMPLE OF CODE IN ACTION:
    * include_by_meta(‘Profile Field Value’, ‘Profile Field Name’), ‘type’ => ‘newest’) )) : ?>
    *
    *
    *
    *
    * Credit: http://www.duable.com
    */

    function include_by_meta($theMetaValue, $theMetaField) {

    $memberArray = array();

    if (bp_has_members()) :
    while (bp_members()) :
    bp_the_member();
    $theFieldValue = bp_get_member_profile_data( ‘field=’. $theMetaField );
    if ($theFieldValue==$theMetaValue) {
    array_push($memberArray, bp_get_member_user_id());
    }
    endwhile;
    endif;

    $theIncludeString=implode(“,”,$memberArray);

    return $theIncludeString;
    }
    ?>
    `

Viewing 5 replies - 1 through 5 (of 5 total)
  • Avatar of landwire
    landwire
    Participant

    @landwire

    Hi there,
    how does your code behave in BP 1.5? I am trying to filter members by user role and used the following code:
    `

    <?php
    $user_role = array (
    ‘role’ => ‘author’,
    );

    $all_authors = get_users ($user_role);

    $members_to_be_included = array ();
    foreach ($all_authors as $all_author) {
    $members_to_be_included[] = $all_author->ID;
    }
    ?>

    `

    The problem I run into though is that when I use the filter members by ‘active’, ‘alphabetical’ etc. those filters would not work anymore. Do they work with your code? Will definitely try this out!

    Avatar of landwire
    landwire
    Participant

    @landwire

    I can still see a problem in making this whole thing dynamic. I.e: have seperate loops for lets say ‘students’, ‘academics’ and ‘staff’. Would that be possible to implement? Basically the values that are passed to inclide_by_meta would have to change according to which name/value/role I want to filter by. That is a bit over my head, as I’m not a coder. Any help is appreciated!

    Avatar of landwire
    landwire
    Participant

    @landwire

    Hi all,

    I modified the code from DuableLLC a bit to check for roles. I post it in case someone is looking for a solution like that:

    This function goes in the functions.php

    `// to exclude only defined roles in the Buddypress Members LOOP

    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;
    }`

    Then in the template members-loop.php you use:
    `$excluded_roles = array (‘administrator’, ‘author’, ‘subscriber’); // this can be any roles you have set up

    if (bp_has_members( ‘exclude=’ . exclude_by_role($excluded_roles) . ‘&’ . bp_ajax_querystring( ‘members’ ) ) ) : ?>
    `

    Avatar of valuser
    valuser
    Participant

    @valuser

    bear with the ignorance !

    `` What do I replace this with ?

    Sorry for not responding to the questions – I never received notification of any replies! If you still need any help, send me a message at mohammad@duable.com and I’ll try to help out the best I can.

    Valuser, 7 months later, I’m sure you have your answer, but that is where you place the code to manipulate/display the information about each user as it runs through the loop.

Viewing 5 replies - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.