Skip to:

Re: Filter members by custom fields



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.


Skip to toolbar