Replies
- Stephanie on June 1, 2024 at 2:10 pm in reply to: [Resolved] How to hide users by user role from the Buddypress Directory #52633
Hello,
Just bumping this thread up so it is not overlooked and forgotten.
- Stephanie on May 27, 2024 at 3:58 am in reply to: [Resolved] How to hide users by user role from the Buddypress Directory #52603
Hello Brajesh,
Your last message landed in my junk folder so I did not see it till now.I am making a job board and want to hide the administrator (ie me) and the job posters so only job seekers are visible in the directory. I have 5 roles in total that I want to hide from the diirectory, including “administrator”, “job_poster”, “student” and two others.
I do not know what their field IDs are and if that is needed but I got the role name from the Members plugin which lists all the user roles on my site.
Also I do not have friends capability added on my site but I left that section of the code in because I am not sure if I can remove it.
To answer your question, here is the exact code I used from the gist page:
Attempt 1, trying to hide multiple roles, specifically “administrator” and “job_poster”:/** * Excludes users from BuddyPress Members list. * * @param string $qs Query string. * @param string $object object loop identikfier. * * @return string */ function bpdev_exclude_users( $qs = false, $object = false ) { if ( $object != 'members' ) { // hide for members only. return $qs; } // comma separated ids of users whom you want to exclude. $excluded_users = join( 'administrator', bpdev_get_excluded_user_ids() ); $args = wp_parse_args( $qs ); // check if we are searching for friends list etc., do not exclude in this case. if ( ! empty( $args['user_id'] ) ) { return $qs; } if ( ! empty( $args['exclude'] ) ) { $args['exclude'] = $args['exclude'] . 'administrator' . $excluded_users; } else { $args['exclude'] = $excluded_users; } $qs = build_query( $args ); return $qs; } add_action( 'bp_ajax_querystring', 'bpdev_exclude_users', 20, 2 ); /** * Retrieves an array of user ids to exclude. * * NOTE:- It is not an efficient idea if you want to exclude thousands of users. There are better ways. * * @return array */ function bpdev_get_excluded_user_ids() { /* // Example:- Multiple roles Exclusion $query = array( 'role__in' => array( 'administrator, job_poster' ), // List of roles to exclude. 'fields' => 'ID' ); */ return get_users( $query ); }
Attempt 2, Limiting display in directory to certain roles only by specifying the single role I want shown (a role called “talent”:
<?php /** * Excludes users from BuddyPress Members list. * * @param string $qs Query string. * @param string $object object loop identikfier. * * @return string */ function bpdev_exclude_users( $qs = false, $object = false ) { if ( $object != 'members' ) { // hide for members only. return $qs; } // comma separated ids of users whom you want to exclude. $excluded_users = join( ',', bpdev_get_excluded_user_ids() ); $args = wp_parse_args( $qs ); // check if we are searching for friends list etc., do not exclude in this case. if ( ! empty( $args['user_id'] ) ) { return $qs; } if ( ! empty( $args['exclude'] ) ) { $args['exclude'] = $args['exclude'] . ',' . $excluded_users; } else { $args['exclude'] = $excluded_users; } $qs = build_query( $args ); return $qs; } add_action( 'bp_ajax_querystring', 'bpdev_exclude_users', 20, 2 ); /** * Retrieves an array of user ids to exclude. * * NOTE:- It is not an efficient idea if you want to exclude thousands of users. There are better ways. * * @return array */ function bpdev_get_excluded_user_ids() { /* // Example: Limiting to certain roles only. $query = array( 'role__not_in' => array( 'talent' ), // List of roles to include. 'fields' => 'ID' ); */ return get_users( $query ); }
I am not a developer. There is a line that says
$excluded_users = join( ',', bpdev_get_excluded_user_ids() );
. Was I supposed to put the roles I want excluded from the directory between the ” injoin(','
? - Stephanie on May 25, 2024 at 7:28 am in reply to: How to deactivate “Media” at the member profile section #52598
Hey Benny,
Do you have a media plugin installed? If you do, deactivate it. I don’t have a media plugin installed so users can’t upload things and there is no “media” button.
- Stephanie on May 22, 2024 at 12:12 pm in reply to: [Resolved] How to hide users by user role from the Buddypress Directory #52576
Hi Brajesh,
Thank you so much for such a speedy reply.
I appreciate that you have updated the code. I only have 5 roles I want to exclude and 1 role I want to include, so I think this piece of code will work for me, but I tried it and it didn’t work for me, it had this problem:
When I tried the “Limiting to certain roles only.”, I added the one role I wanted to include in my directory and when I opened the directory, the page stays stuck on the “Loading the members of your community. Please wait.” message, and shows there are 6 members (when there should be 2 members, both with the same role I wanted to include). The same problem occured when I manually entered the 5 roles I want to exclude. I got the user roles from my wordpress dashboard.
Also, I noted that you mentioned the other BuddyDev article with the code for excluding certain users, I used the code from the github page. Not sure if I was supposed to use the BuddyDev one?
Thank you
Stephanie- This reply was modified 10 months, 2 weeks ago by
Stephanie. Reason: Add detail
- This reply was modified 10 months, 2 weeks ago by