Support only during business hours : Monday to friday, from 8:30 am – 5:30 pm CEST

Due to the decrease in our staff due to vacations, our response time may be longer.

Be sure we're doing our best to manage your topic as soon as possible.

Restrict directory by gender

This topic is resolved
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • Malas Reda
    Participant
    • 13 Topics
    • 28 Posts
    @malasreda

    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 ?

    Themosaurus
    Keymaster
    Themosaurus Support
    • 1 Topics
    • 1675 Posts
    @themosaurus

    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:

    Adding Custom PHP Without Changing Your Child Theme

    Best regards,

    Malas Reda
    Participant
    • 13 Topics
    • 28 Posts
    @malasreda

    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.

    Themosaurus
    Keymaster
    Themosaurus Support
    • 1 Topics
    • 1675 Posts
    @themosaurus

    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,

    Malas Reda
    Participant
    • 13 Topics
    • 28 Posts
    @malasreda

    Perfect it works !

    Thank you

    Themosaurus
    Keymaster
    Themosaurus Support
    • 1 Topics
    • 1675 Posts
    @themosaurus

    You’re welcome @malasreda 🙂

    Best regards,

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Restrict directory by gender’ is closed to new replies.

Troubleshooting Demo Imports

You're trying to setup your theme but you're experiencing errors when importing the demo content? Or you've just followed the setup guide but your website doesn't look exactly like our demo? These are common issues for which you can find easy and quick fixes.

Happy With our Support So Far?

Feel free to review our theme on Themeforest! It helps us making our products more known to new potential customers, which allow us more time to improve the quality and develop new features. #SharingIsCaring ❤️

Discover MatchPress

Skip • Like • Super-Like

Add powerful matching features like Member likes, skips, super likes, conditional private messaging and much more.

Setup Your Cera or Gwangi powered Community Website and Turn it into an iOS and Android App

15% discount for Cera users

To unleash the full power of your Cera or Gwangi theme, we have partnered with the Zipline team. Your community website can now be fully setup and turned into your very own custom app for iOS and Android.

Whether you've newly acquired the theme or already got your site up and running, Zipline got you covered. And we got you an incredible discount.

Holiday, Weather & Festive effects
to pimp your WordPress Site