Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to include only one user role in the members directory?


  • CloudedDottedMind
    Participant

    @cloudeddottedmind

    Hi,

    I’m trying to make it so only one WP user role is shown in the Bp directory.

    I could only find a code to exclude IDs but that would mean new roles, later on, would show. Any way to make it so only the one role is included (users might have multiple roles but so long as they have that one they show)?

Viewing 1 replies (of 1 total)

  • CloudedDottedMind
    Participant

    @cloudeddottedmind

    I figured it out… it was pretty simple in the end as all I did was change ‘role” to ‘role__not_in’ while putting the role I do want to show in $role

    /**
     * Exclude Users from BuddyPress Members List unless they have specific WordPress role.
     *
     * @param array $args args.
     *
     * @return array
     */
    function buddydev_exclude_users_by_role( $args ) {
        // do not exclude in admin.
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
            return $args;
        }
     
        $excluded = isset( $args['exclude'] ) ? $args['exclude'] : array();
     
        if ( ! is_array( $excluded ) ) {
            $excluded = explode( ',', $excluded );
        }
     
        $role     = 'roletoshow';// change to the role to be shown.
        $user_ids = get_users( array( 'role__not_in' => $role, 'fields' => 'ID' ) );
     
        $excluded = array_merge( $excluded, $user_ids );
     
        $args['exclude'] = $excluded;
     
        return $args;
    }
     
    add_filter( 'bp_after_has_members_parse_args', 'buddydev_exclude_users_by_role' );
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.
Skip to toolbar