Skip to:

Filter members by custom fields

  • buzz2050



    This is my first time with BP and my very first post in the BP forums. I have a query related to the members-loop.

    We have grouped our bp members role-wise. They choose a role from a set of four predefined roles while signing up (it’s a custom profile field).

    I have provided 4 tabs for 4 roles on my members page and clicking on a particular tab should only show the members belonging to THAT role. How can this elegantly be achieved with BP 1.1? Is there any template tag available?

    I basically want to collect the members-array that is returned by bp_site_members() and further filter it by ‘role’.

    I suppose I can hack the core, or maybe add my own function in bp-custom.php file to retrieve the members by whatever clause I want or maybe there is already a way in BP of doing this that I’m just not aware of.

    Not sure, if this is much simpler in logic than in my head right now.

    Any help much appreciated



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

  • buzz2050


    Hi, I tried to achieve this by putting an ‘if’ in the members-loop which is basically called for each tab. For each corresponding role tab, I check if the current member in the while loop belongs to that role, if yes, only then I print that member under that role tab.

    I did not change the core for this, instead I changed the index.php of my BP theme and also added a bp-custom.php with some customized code. Not sure if this is the most elegant way of doing this. Was there any hook or something which I could’ve used maybe?




    That looks like a pretty good solution. Can you share the code with us and how you did it? I need exactly the same thing! :-)



    Well, I would be happy to share the solution, but I don’t really think my way is all that elegant, there’s got to be a better way and I’m waiting for someone to suggest it to me.

    Meanwhile, I am still using the old 2-theme model. I made some customizations in my bpmember theme to achieve this and put in some code in plugins/bp-custom.php as well.

    Firstly, we have a custom profile field called ‘Role’. And we wanted to have separate Role-tabs (as you can see on our site) to list all members for THAT role.

    To achieve this, I completely changed the bpmember/directories/members/index.php code and put in my ‘Role’ tab-structure there. Then, instead of making the members index file call the members-loop, I wrote a modified version of the members-loop, made it as a function and put it in my bp-custom.php. Now I make a call to this function (which is basically the members-loop) from my bpmember/directories/members/index.php page.

    To give you an eg of how we display all companies under the Company tab:

    In my bpmember/directories/members/index.php, under the code section for ‘Company’ tab I make a call like:


    In my bp-custom.php, I have written a function display_members_by_role($role) which is nothing but a slightly modified version of the members-loop.

    Here, in the ‘while’ for members-loop, I check for the Role field for that user-id. For this eg, if the Role is ‘Company’, print that member in the company-listing, else not.

    This is the code I have put in the members-loop which checks the custom profile field (which is this case is ‘Role’) and it’s value for that user-id:

    <?php while ( bp_site_members() ) : bp_the_site_member(); ?>

    global $site_members_template ;

    $arr = BP_XProfile_ProfileData::get_value_byfieldname(array('Role'), $site_members_template->member->id) ;

    if( $arr['Role'] == $role) //$role is the role value received by the function, in this eg - Company
    //do whatever
    <?php endwhile; ?>

    Same logic is used for all other roles.

    BTW, get_value_byfieldname() can return values for multiple fields too. In case you want to retrieve values of more than one profile fields, its easily possible. Say I want the ‘City’ custom field value too, then I would pass something like –

    $arr = BP_XProfile_ProfileData::get_value_byfieldname(array(‘Role’,’City’)

    and access the ‘City’ value using $arr[‘City’]

    This function is pretty handy.

    While I am able to print the members belonging to that corresponding role using this logic, what I can’t get into place is the pagination part. The pagination still takes into account ‘All’ members since the members-loop technically does retrieve all members.

    All I have done is put a condition in order to just get the members for that role displayed. I was wondering if I should put in a separate pagination module, or if there is any other way of achieving this whole thing.




    Ok, thanks! I’ll try it out and see how it goes. I would love a plugin that could do all of this. There’s a premium plugin called “Members Directory”, but it doesn’t filter. :-/

    Creative Slice


    Sib, thanks for this info. Do you happen to know how I could use a function like this to show members by their Role? For example I could have 1 page showing “Subscribers”, another “Editors” and another “Admins”?


    bineet chaubey


    hi buzz2050

    i want to add some sub menu in member page as you show in your attachment,

    and filter memebr by role please tell me how we can do this if any code have you
    for this scripts . please told me . i am highly appreciated your help.
    please tell me how we van set querystring in ajax.php to filter memeber by role and add submenu in member page

    please help me.



    Hi all,
    looking to exactly do this. Filter members by user role! Can anyone share any code? I think that would be a great addition to the BP core anyway!!!
    @buzz2050 Have you got any code to share?



    Is anyone still watching this? Would love to get this functionality working!!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Filter members by custom fields’ is closed to new replies.
Skip to toolbar