BuddyDev

Search

[Resolved] How to get opposite member type with common items selected?

  • Participant
    Level: Initiated
    Posts: 4
    Anjan Phukan on #43783

    Hi,

    I am very new to buddypress.

    I need to develop a website where there will be 2 member types – “Donor” and “Charity”. On the registration page both of them will select some items either to donate or to get donated.

    Now after login one member type will see a list of members from the member type but with common donation items in a wordpress page.

    For example,
    Donor1 selected Item1, Item2
    Charity1 selected Item1, Item2,
    Charity2 selected Item2, Item3
    Charity3 selected Item3, Item4
    Then Donor1 will see only Charity1 and Charity2 because they have the common items.

    For this I want to use Member Loop – bp_has_members(). I think those items are stored as array.

    Now can anyone help me with this? How can I write the code so that the array of items are checked in the Member Loop and I get the desired result?

    Thank you so much.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #43802

    Hello Anjan,

    Welcome to the BuddyDev Forums. Please take a look at the following blog it explains how to show members based on member type.

    https://buddydev.com/show-only-users-of-opposite-genders-on-buddypress-site/

    Regarding your second requirement for common items. I am supposing you are using Xprofile fields for this. If yes, you can use the Xprofile query within the member’s loop.

    Please give it a try.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 4
    Anjan Phukan on #43805

    Hi Ravi

    Thank you so much for the suggestion.

    Can I have an example how BP_XProfile_Query works with a custom field with checkboxes? I think BP_XProfile_Query works will with textbox type fields as the values are single values. But with a checkbox field the values are stored in the database in a serialized array. You can check the screenshot here:
    https://ibb.co/BgMRFw1

    I also tried the my_custom_id() function mentioned in Members Loop page: https://codex.buddypress.org/developer/loops-reference/the-members-loop/
    But it also works well with textbox fields.

    I am struggle to find out a solution for checkboxes.

    Any suggestions please?

    Regards,
    Anjan

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #43806

    Hello Anjan,

    Please have a look at the “BP_Xprofile_Queury” class. They have mentioned in the doc comment all supported args.

    https://github.com/buddypress/buddypress/blob/master/src/bp-xprofile/classes/class-bp-xprofile-query.php#L54

    You will have a better idea to use this. Please let me know if you need further assistance.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 4
    Anjan Phukan on #43807

    Hi Ravi,

    Thank you.

    So do you mean something like this will work?

    
    $args = array(
       'member_type' => 'donor',
       'xprofile_query' => array(
          'relation' => 'AND',
          array(
            'field_id' => $fieldID,
            'value' => $arrayItems,
            'compare' => 'IN',
          )
       ),
    );
    $user_query = new BP_User_Query( $args );
    

    Or do I need to make any adjustments?

    Regards,
    Anjan

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #43810

    Hello Anjan,

    Thank you for sharing the code. No need to add the ‘relation’ key to the Xprofile query.

    
    
    $args = array(
    	'member_type' => 'donor',
    	'xprofile_query' => array(
    		array(
    			'field_id' => $fieldID,
    			'value' => $arrayItems,
    			'compare' => 'IN',
    		),
    	),
    );
    
    

    You can directly pass this args to the member query. There is no need to create a User_Query instance. Modify the filter_args method from the above mention blog code.

    Regards
    Ravi

    • This reply was modified 2 years, 8 months ago by Ravi.
  • Participant
    Level: Initiated
    Posts: 4
    Anjan Phukan on #43822

    Hi Ravi,

    I was almost sure that it will work, but it didn’t so I made these changes in the code and it worked for me now. But I am not sure yet how time taking it will be when there will be more items in the array:

    $xprofile_query = array( 'relation' => 'OR');
    	
    foreach ($my_items as $key => $value) {
    	$xprofile_query[] = array( 
    		'key'=> $fieldID, 
    		'value' => $value,
    		'compare' => 'LIKE' 
    	);
    }
    		
    $args = array(
    	'member_type' => 'donor',
    );
    	
    if ($xprofile_query){
            $args['xprofile_query'] = $xprofile_query;
    }
    	 
    $user_query = new BP_User_Query( $args );
    

    Do you have any suggestion?

    Thank you.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #43831

    Hello Anjan,

    Thank you for the acknowledgement. Yup, The Xprofile query is slow for the large data set. Can you please share the complete code so that I can give a better review?

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 4
    Anjan Phukan on #43834

    Hi Ravi,

    Thank you for your help.

    So here is my another post where I explained the requirement in a little detail:
    https://buddypress.org/support/topic/filter-members-based-on-checkbox-type-custom-field/#post-323838

    So, both the member types need to select the checkboxes for the Items and then, when a user is logged in, I need to show the opposite member types with common items on a WordPress page template.

    First, I need to get the opposite member type of the logged in user and then the Items he has selected at the time of registration:

    
    if ( is_user_logged_in() ) {
    	$current_user_id = get_current_user_id();
    	$current_member_type = bp_get_member_type( $current_user_id );
    
            $current_member_items = xprofile_get_field_data( 'Items', $current_user_id ); // IT GIVES AN ARRAY OF SELECTED ITEMS
    }
    	
    if ( 'charity' === $current_member_type ) {
            $opposite_member_type = 'donor';
    } elseif ( 'donor' === $current_member_type ) {
            $opposite_member_type = 'charity';
    }
    

    Then the next query part:

    
    $xprofile_query = array( 'relation' => 'OR');
    	
    foreach ($current_member_items as $key => $value) {
    	$xprofile_query[] = array( 
    		'key'=> 'Items', 
    		'value' => $value,
    		'compare' => 'LIKE' 
    	);
    }
    		
    $args = array(
    	'member_type' => $opposite_member_type,
            'type' => 'newest',
            'per_page' => 10
    );
    	
    if ($xprofile_query){
            $args['xprofile_query'] = $xprofile_query;
    }
    	 
    $user_query = new BP_User_Query( $args );
    
    $users = $user_query->results;
    
    if($users){
            echo $member_ID = $user->ID;
            echo $user->fullname; // WILL SHOW THE USER DETAILS WITH HTML
            
            $items = xprofile_get_field_data( 'Items', $member_ID );
    }
    

    I hope this will give you an idea about the requirement. If you think there is a better way to do this then please let me know, as I am completely new to Buddypress.

    I have a few questions if you don’t mind:

    1/ How can I add pagination with BP_User_Query? If I use “Members Loop” with bp_has_members() then I can easily use the predefined functions to get the pagination, count, user name, permalinks, etc.
    https://codex.buddypress.org/developer/loops-reference/the-members-loop/

    2/ Can I add “xprofile_query” or any “meta_query” array in bp_has_members()? If possible then it will be easier to to use those predefined functions to show pagination, count, etc.

    Any thoughts on these?

    Thank you so much.
    Anjan

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #43840

    Hello Anjan,

    Thank you for the detailed reply. You do not need BP_User_Query in favour of the BuddyPress Members loop. You can achieve the same thing we members’ loop.

    Member’s loop does support Xprofile query and meta query in case of meta_query it does support only single key.

    Please check

    Regards
    Ravi

The topic ‘ [Resolved] How to get opposite member type with common items selected?’ is closed to new replies.

This topic is: resolved