It’s another day at my job, answering tickets as usual. There is always this question that keeps popping every-time. “How to display BuddyPress members directory alphabetical?.”
I already answered this questions many times in the past so I decided to put it in a place where I can link an answer to the ticket and eventually hoping that this will appear in the search engine when people start looking for an answer saving their time and solving their problem.
BuddyPress by default sorts the items of the members (e.g., yoursite.com/members) directory of your website by “Active.” This behavior is suitable for most users, but if you are like me who likes to display the members’ directory alphabetically by default, then you need to get your hands dirty with code.
As of this writing, I cannot find any plugins that do the job well, but if you know, a plugin that does this then link the plugin URL into the comment section below.
Adding Some Code in bp-custom.php
Type or copy the following code into the text editor of your choice:
Save it as bp-custom.php after you have copied and pasted the code above.
In case you have already a bp-custom.php file located in wp-content/plugins/bp-custom.php, just add the code above but make sure you omit the PHP opening tags ‘<?php’.
Otherwise, upload the file (bp-custom.php) into your sites wp-content/plugins directory using any FTP clients if no bp-custom.php file exists in your server.
Refresh the page after you have finished uploading the file. Check to see if your buddypress members directory displays the list in alphabetical order now.
So what exactly is this file bp-custom.php and how does it work?
bp-custom.php is a file that resides in your WordPress ‘plugins’ folder where you can add a bunch of custom code hacks and modifications to BuddyPress.
All right! What the code above does is we hooked our custom function called “my_bp_loop_querystring” (feel free to change the prefix ‘my_’ to something).
Inside “my_bp_loop_querystring“, we added a parameter ‘&type=alphabetical‘ which is a string represented by $query_string variable. The format is somewhat similar to the URL parameters we used to get $_GET variables.
You can also pass parameters like type, page, per_page, max, user_id, search_terms, include, populate_extras, exclude ,meta_key ,meta_value, page_arg, member_type, member_type__in, and member_type__not_in to customize the display of your BuddyPress Members Directory.
As an example, if you want to limit the number of displayed members in your Buddypress Members Directory, you would need to add the following code in line #10 of the given code above:
$query_string .= '&max=3';
What the code does is concatenate the parameter ‘max‘ with a value of ‘3‘ to our existing ‘query string.’ This line of code will limit the maximum number of members in BuddyPress Members Directory to three.
Feel free to play with other parameters.
Thankfully, BuddyPress offers useful action hooks that we can use to modify the flow of the plugin.
Let me know in the comments below if you encounter any issues.