BuddyDev

Search

[Resolved] Show Opposite Gender Base on XProfile

  • Participant
    Level: Initiated
    Posts: 10
    Catherine F. Weah on #46621

    Hello, hope I can get some assistance please.

    I followed the tutorial here https://buddydev.com/show-only-users-of-opposite-genders-on-buddypress-site/ , and did some modification so that it works with the xprofile field, precisely gender in buddyboss and I have only male and female.

    
    // Modifies members loop args to filter out users of same gender.
    	public function filter_args( $args ) {
    		// do not filter if user is not logged in, or is super admin or we are inside dashboard.
    		if ( ! is_user_logged_in() || is_super_admin() || ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) {
    			return $args;
    		}
    
    		$opposite_gender = $this->get_opposite_gender( bp_loggedin_user_id() );
    		$jlw_gender_field = xprofile_get_field( bp_get_xprofile_gender_type_field_id() );
    		if ( $opposite_gender ) {
    			$args[$xprofile_query] = $opposite_gender;
    		}
    		return $args;
    
    	}
    
    	// Restricts access to user profile.
    	// Only allows profile owner, user of opposite gender or site admins to view.
    	public function check_access() {
    		if ( ! is_user_logged_in() || ! bp_is_user() || bp_is_my_profile() || is_super_admin() ) {
    			return;
    		}
    
    		$user_gender = $this->get_user_gender( bp_loggedin_user_id() );
    		$displayed_user_gender = $this->get_user_gender( bp_displayed_user_id() );
    
    		// do not allow access for same gender.
    		if ( $displayed_user_gender === $user_gender ) {
    			bp_core_add_message( __( 'Access denied.', 'yym-helper' ) );
    			bp_core_redirect( bp_loggedin_user_domain() );
    		}
    	}
    
    	// Get user gender
    	private function get_user_gender( $user_id ) {
    		return xprofile_get_field_data( bp_get_xprofile_gender_type_field_id(), $user_id );
    	}
    
    	// Finds the opposite gender
    	private function get_opposite_gender( $user_id ) {
    
    		$user_gender = $this->get_user_gender( $user_id );
    
    		if ( empty( $user_gender ) ) {
    			return null;
    		}
    
    		if ( $user_gender === 'his_Male' ) {
    			return 'her_Female';
    		} elseif ( $user_gender === 'her_Female' ) {
    			return 'his_Male';
    		}
    
    		return null;
    	}
    

    Now the code only works if a member profile is visited, and will redirect back if it is the same gender, but on the member directory it doesn’t work. All the members are still showing.

    I tried to dig a bit into it, but it turns out that I am not a developer to figure it out.

    I feel the problem is here
    $args['member_type'] = $opposite_gender;
    replaced by
    $args[$xprofile_query] = $opposite_gender;
    still it doesn’t work; and then I discover this https://www.buddyboss.com/resources/reference/functions/bp_xprofile_add_xprofile_query_to_user_query/ , but don’t know how to use it.

    Some help please.
    Thanks in advance

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #46653

    Hi Catherine,
    Welcome to BuddyDev support forums.
    I am sorry, I could not reply earlier.

    Please share the complete code on pastebin.com and link me here. I will check and update that.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 10
    Catherine F. Weah on #46655

    Hi Brajesh,
    Thanks a lot for the kind response.
    Here’s the link to the code https://pastebin.com/8KqPyKuB

    Once again, thank you very much in advance.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #46661

    Hi Catherine,
    Thank you.

    Cna you please try using this and see if it works?
    https://pastebin.com/mEPhWikK

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 10
    Catherine F. Weah on #46666

    Hi Brajesh,

    Thanks a lot for the response, this is really a great forum.
    The code works well on my local host, but is not working on my live server, however, I think it is caching issue, so I am going to give it some time to take effect, and will definitely let you know when it finally works.

    Once again, thank you very much.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #46667

    Hi Catherine,
    Thank you for the reply.
    I am glad that it worked on your local setup.

    Not sure why it is not working on the live server, is there any other plugin using xprofile_query? That could be the reason.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 10
    Catherine F. Weah on #46669

    Hi Brajesh,
    It was actually caching issue, now it is also working on my live server.
    Just to know, what is I want to do the XProfile Query with additional 1 or 2 more xprofile field(s).
    How do I add them?

    Thanks for all the assistance

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #46679

    Hi Catherine,
    Thank you.

    I am glad that it worked.

    You can change this

    
    $query->query_vars['xprofile_query'] = array(
    	array(
    		'field'   => bp_get_xprofile_gender_type_field_id(), // field id or name.
    		'value'   => $opposite_gender,
    		'compare' => '=',
    	),
    );
    
    

    with something like this.

    
    
    $query->query_vars['xprofile_query'] = array(
    	array(
    		'field'   => bp_get_xprofile_gender_type_field_id(), // field id or name.
    		'value'   => $opposite_gender,
    		'compare' => '=',
    	),
    	array(
    		'field'   => 'your_field_id', // field id or name.
    		'value'   => 'the value',
    		'compare' => '=', // your compare operator.
    	),
    
    );
    
    

    You can add as many conditions as you need but I will recommend to be cautious. Xprofile query is not efficient and if you put 4-5 xprofile query clauses, it may cause database load issues. I have seen the issue on a site with moderate number of users couple of years ago.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 10
    Catherine F. Weah on #46680

    Hi Brajesh,
    I will take your advise.
    I just wanted to know. Perhaps in the nearer future I may have a need for that.

    Thank you very much for the assistance; and also thanks to you and the BuddyDev team for considering my appeal on the duplicated topic and deleting one.

    I think this topic very much now resolved.
    Once again, thank you very much.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #46702

    Hi Catherine,
    You are welcome.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: resolved