BuddyDev

Search

Replies

  • 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

  • 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.

  • 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

  • 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