Skip to:
Content
Pages
Categories
Search
Top
Bottom

Filter members based on checkbox type custom field


  • anjanphukan
    Participant

    @anjanphukan

    How to use bp_has_members() function to filter members based on a custom field which is a set of checkboxes?
    I think checkbox values are stored as array in database.
    The my_custom_ids() function in Members Loop works only with single value. But it doesn’t work with array of checkboxes.

    Thank you.

Viewing 8 replies - 1 through 8 (of 8 total)

  • shanebp
    Moderator

    @shanebp

    Show us the checkbox values as retrieved from the database.


    anjanphukan
    Participant

    @anjanphukan

    When the checkboxes are selected at the time of registration, they are stored in “bp_xprofile_data” table like this:
    IMG-20220312-WA0000


    shanebp
    Moderator

    @shanebp

    I can provide some tips – but you need to be able to write some php code.
    Show an example of the filter parameters you want to pass to the ids function.
    my_custom_ids( $filter_parameters );


    anjanphukan
    Participant

    @anjanphukan

    Sure no problem. So here is my scenario.

    There are 2 member types and I have a custom user field created, let’s called it Items.

    Both member types need to select some of those Item checkboxes. Now I need to show the oposite member types with common Items on a member profile. For that I created a custom page and run the Members loop. I was able to show the oposite members. But I am not sure how to match the Items and filter those oposite members. The my_custom_id() function works well if it is a textbox field. But it doesn’t work with the checkboxes. You can see how they are stored in the database.

    I can get the current logged in user’s Items list in an array format. I think if I follow the my_custom_id() function then I will have to pass the field name and the array to compare like this my_custom_id($item_field, $current_user_items_array)

    I think with that the SQL select query in the function needs to be modified.

    Hope this clarifies my requirement.

    I am very new to buddypress. If there is another way to achieve this then I will be happy to implement. My main goal is to show the members with common items selected but from the opposite member type to a logged in member.

    Thank you so much.


    shanebp
    Moderator

    @shanebp

    If you can get the current user’s items field, then you know the field id.
    So $item_field would be that id.
    Because the database field is a serialized array, you need to do a string search on that field.
    ( String searches on serialized arrays are slow – especially for multiple terms )
    Yes, you need to change the sql to handle the array from that user.
    There is lots of info on the web re handling multiple terms for a LIKE statement.

    You need to loop thru the values in the $items_field and create a string something like this:
    AND value LIKE '%" . $item_1 . "%' OR value LIKE '%" . $item_2 . "%'… etc


    anjanphukan
    Participant

    @anjanphukan

    Thank you so much @shanebp.

    But I am a little bit worried as this may bring up the results for the items named – “tested”, “pre-test”, “untest”, “not tested”, etc. if I try to filter the items named only “test”.

    Any suggestions?

    Regards.


    shanebp
    Moderator

    @shanebp

    The only scalable method is to store the user’s item selections as separate rows in the database.
    Then you can remove the wildcards from the sql.


    anjanphukan
    Participant

    @anjanphukan

    I am using BP_User_Query and BP_Xprofile_Queury with the following code and it worked for me now.

    $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 );

    Thank you.

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.
Skip to toolbar