Restrict by gender

This topic contains 1 reply, has 2 voices, and was last updated by  Themosaurus 2 months, 2 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #5008

    Malas Reda
    Participant
    • 15 Posts
    @malasreda

    Hey,

    You have already gave me this snippet wich works perfectly:

    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;
    }

    But when i have changed the field “what are you looking for ?” to a check box with multiple choice instead of radio, this snippet won’t work any more !
    Is there any solution to make it work?

    This is the previous topic wich is closed now :https://support.themosaurus.com/forums/topic/restrict-directory-by-gender/

    #5016

    Themosaurus
    Keymaster
    • 1669 Posts
    @themosaurus

    Hi @malasreda,

    That snippet is indeed not designed to work with checkbox fields. However I think it should work with a small change:

    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
    	$genders = xprofile_get_field_data( $field_id, bp_loggedin_user_id() );
    	$genders = esc_sql( $genders );
    	$genders = implode( "','", $genders );
    
    	if ( ! empty( $gender ) ) {
    		$query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = {$filter_field_id} AND value IN ('{$genders}')";
    	}
    
    	$custom_ids = $wpdb->get_col( $query );
    
    	$args = wp_parse_args ( $qs );
    	$args['include'] = implode ( ',', $custom_ids );
    	$qs = build_query ( $args );
    
    	return $qs;
    }

    Please try to replace the snippet I previously gave you by this one and keep me updated.

    Best regards,

Hi there,

This topic has been inactive for a while now so we will be closing it to keep the forum tidy. Don't hesitate to create a new topic if you still need help and we'll be glad to help you!

Best regards,
The Themosaurus team.

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

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