BuddyDev

Search

[Resolved] Hiding User ONLY when it has ONE role on BuddyPress-based site

  • Participant
    Level: Initiated
    Posts: 1
    Hastibe on #32747

    This code snippet (https://buddydev.com/hiding-users-on-buddypress-based-site/) works great to hide users who have a specific role (among other roles), but is there a way to modify it so that ONLY users who have ONE role are hidden?

    In other words, if we want to only hide users with the “subscriber” role, but still show users that have BOTH the “subscriber” role AND another role (e.g. “member”), how could we do this?

    Alternatively, another option that would work for us is to hide all users, except for any user that has the “member” role (even if they also have other roles). If this is easier to do, could anyone help us with this code?

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #32750

    Hello Hastibe,

    Thank you for posting. Try the following it will include only users with subscriber role.

    
    
    /**
     * Exclude Users from BuddyPress Members List by WordPress role.
     *
     * @param array $args args.
     *
     * @return array
     */
    function buddydev_include_users_by_role( $args ) {
    	// do not exclude in admin.
    	if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
    		return $args;
    	}
    
    	$included = isset( $args['include'] ) ? $args['include'] : array();
    
    	if ( ! is_array( $included ) ) {
    		$included = explode( ',', $included );
    	}
    
    	$role = 'subscriber';// change to the role to be include.
    
    	$user_ids = get_users(
    		array(
    			'role'   => $role,
    			'fields' => 'ID',
    		)
    	);
    
    	$included = array_merge( $included, $user_ids );
    
    	$args['include'] = $included;
    
    	return $args;
    }
    
    add_filter( 'bp_after_has_members_parse_args', 'buddydev_include_users_by_role' );
    
    

    Please let me know if is works or not.

    Regrads
    Ravi

  • Participant
    Level: Initiated
    Posts: 1
    Hastibe on #32752

    That works! You’re my hero, Ravi–I was getting nowhere with this, and you just made my day. 🙂 Thank you SO much for your help!

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #32761

    Hello Hastibe,

    Thank you for the kind words. I am glad that I could help.

    Regards
    Ravi

You must be logged in to reply to this topic.

This topic is: resolved