Thursday 17 August 2017

Display Members in BuddyPress by Roles


In a recent project, I had to create a custom BuddyPress member list. I had to display members by their membership. So, I wanted to write a simple tutorial in which I’ll show you how to display members by their roles. In this article, you’ll learn how to create a custom member tab and how to filter only the users we want. This tutorial will also require some customization of the BuddyPress Member list template, but I’ll show you that at the end of this article.
In this tutorial, we will show users that have the Administrator role, but you can easily copy or edit the code to show other users.
You can put this code in your theme or in a plugin. For this projects, which was a specific theme, I have put this in a child theme.
Getting the User IDs
Let’s define the function that will return an array of user IDs that we can then use for filtering.
<?php
/**
* Get only User IDs which have the administrator role
* @return array +
*/
function bp_get_only_administrators_ids() {
$user_ids = get_transient( ‘bp_only_administrators_ids’ );
if( false === $user_ids ) {
$args = array(
‘role__in’ => ‘administrator’,
‘fields’ => ‘ID’
);
$user_ids = get_users( $args );
set_transient(
Source: https://managewp.org/articles/15884/display-members-in-buddypress-by-roles




source https://williechiu40.wordpress.com/2017/08/17/display-members-in-buddypress-by-roles/

No comments:

Post a Comment