BuddyDev

Search

[Resolved] How to exclude users without member type from directory

  • Participant
    Level: Initiated
    Posts: 2
    Mathilde Gauvain on #32130

    Hi,

    I purchased your member types plugin because I need the directory to only show members, not all WordPress users. I don’t understand how to do this – can you help me?

  • Participant
    Level: Initiated
    Posts: 2
    Mathilde Gauvain on #32132

    In case it helps someone else:
    For the time being, I added a WP role “Community Member”, linked it to the membership purchase, then added this code to functions so the directory only shows users with this 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     = 'community_member';// 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' ); 
  • Keymaster
    (BuddyDev Team)
    Posts: 25058
    Brajesh Singh on #32137

    Hi Mathilde,
    Welcome to BuddyDev forums.

    Thank you for using the plugin. Your approach of using a role is much better. Another solution is to exclude users without a member type(by setting member_type__in option). For now, I will suggest keep using it.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 2
    Mathilde Gauvain on #32138

    Thanks Brajesh, that makes sense!

  • Keymaster
    (BuddyDev Team)
    Posts: 25058
    Brajesh Singh on #32146

    Thank you.

The topic ‘ [Resolved] How to exclude users without member type from directory’ is closed to new replies.

This topic is: resolved