Find Answers and Get Support › Forums › Gwangi – PRO Multi-Purpose Membership, Social Network & BuddyPress Community Theme › Popular Features › Members Directory › Restrict directory by gender
- This topic has 5 replies, 2 voices, and was last updated 5 years, 11 months ago by ThemosaurusVerified.
-
AuthorPosts
-
Malas RedaParticipant@malasreda
- 13 Topics
- 28 Posts
In the registration form, i have included a field “what are you looking for”. If the user selected “male”, he must have access to only male members and the other genders are restricted.
How can i make this possible ?
January 28, 2019 at 15:01 #4853@themosaurus- 1 Topics
- 1675 Posts
Hi @malasreda,
Here is a PHP code snippet that should help you:
add_filter ( 'bp_ajax_querystring', 'gwangi_modify_members_loop', 20, 2 ); function gwangi_modify_members_loop( $qs = false, $object = false ) { global $wpdb; if ( $object != 'members' ) return $qs; $field_id = xprofile_get_field_id_from_name( 'What are you looking for' ); // figure out if the logged-in user is male or female $gender = xprofile_get_field_data( $field_id, bp_loggedin_user_id() ); if ( ! empty( $gender ) ) { $query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = {$field_id} AND value = '{$gender}'"; } $custom_ids = $wpdb->get_col( $query ); $args = wp_parse_args ( $qs ); $args['include'] = implode ( ',', $custom_ids ); $qs = build_query ( $args ); return $qs; }
Don’t forget to change
What are you looking for
in the code snippet by your own field name.As a reminder, if you need to find out how to add a PHP code snippet to your site, we recommend that you visit this article of our documentation:
Best regards,
January 28, 2019 at 15:36 #4864Malas RedaParticipant@malasreda- 13 Topics
- 28 Posts
Hello,
I have tried to copy and past this snippet, it shows me only male although i have selected i am searching for females.
The website is in french, so i have two fields aded :
Field-1 : what are you looking for ? “5 options”
Field-2 : What gender are you ? “same 5 options as field 1”exemple: the user logged in is looking for a female and he is a male, he should only see females. If she is a female looking for females she should only see females. ( the options are : female, male , female/female couple, male/female couple, male/male couple)
So the field 1 of the logged in user must be equal to the field 2 of the members to be shown in the member directory.
Is it posssible ?
Thank you in advanced.
January 29, 2019 at 00:38 #4888@themosaurus- 1 Topics
- 1675 Posts
Hi @malasreda,
Ok, I understand the issue. Here is the updated snippet:
add_filter ( 'bp_ajax_querystring', 'gwangi_modify_members_loop', 20, 2 ); function gwangi_modify_members_loop( $qs = false, $object = false ) { global $wpdb; if ( $object != 'members' ) return $qs; $field_id = xprofile_get_field_id_from_name( 'What are you looking for ?' ); $filter_field_id = xprofile_get_field_id_from_name( 'What gender are you ?' ); // figure out if the logged-in user is male or female $gender = xprofile_get_field_data( $field_id, bp_loggedin_user_id() ); if ( ! empty( $gender ) ) { $query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = {$filter_field_id} AND value = '{$gender}'"; } $custom_ids = $wpdb->get_col( $query ); $args = wp_parse_args ( $qs ); $args['include'] = implode ( ',', $custom_ids ); $qs = build_query ( $args ); return $qs; }
Don’t forget to change
'What are you looking for ?'
and'What gender are you ?'
in the code snippet by your own fields names.Regards,
January 29, 2019 at 12:16 #4905Malas RedaParticipant@malasreda- 13 Topics
- 28 Posts
Perfect it works !
Thank you
January 30, 2019 at 15:32 #4960@themosaurus- 1 Topics
- 1675 Posts
January 30, 2019 at 16:55 #4973 -
AuthorPosts
The topic ‘Restrict directory by gender’ is closed to new replies.