BuddyDev

Search

Featured member plugin – add extra filter option(s)

  • Participant
    Level: Initiated
    Posts: 6
    Melanie on #51638

    Hi,

    in BuddyBoss there is this dropdown with those extra 3 filter options: “Most recently active”, “Newest” and “Alphabetical”.

    I have read this artcile in the BuddyPress codex: “Add custom filters to loops and enjoy them within your plugin” and it helps to add extra filters into the filter dropdown but it doesnt mention how to adjust the logic accordingly.

    I have tried with ChatGPT to come up with a solution to modify the logic for an additional filter, e.g. show only those members whose name starts with an “A”. But to no avail.

    What I wanted to achieve is to use the “Featured Member” plugin by Buddydev which shows the featured members in the results/member page. Now I tried to add an extra filter to sort them in a different way but as I mentioned all I achieve is I can make the new filter option show up in the dropdown but the logic doesnt work.

    I would be happy if you can help or show me in the right direction 🙂

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2934
    Ravi on #51650

    Hello

    BuddyBoss only supports ‘active’, ‘random’, ‘newest’, ‘online’ and ‘alphabetical’ types. Please let me know which filter you are trying to implement. Please also share the code you use to add a new filter to the dropdown.

    Regards
    Ravi

    • This reply was modified 2 months, 1 week ago by Ravi.
  • Participant
    Level: Initiated
    Posts: 6
    Melanie on #51660

    Hi Ravi,
    sorry seems my 2nd message with the code did not get sent.
    Yes, these filter options are the native filter options we get:
    ‘active’, ‘random’, ‘newest’, ‘online’ and ‘alphabetical’.
    What I tried to achieve was to add a new one. Or if its not possible to add a new filter option in this dropdown I would also be happy to show it as a tab (next to “All Members”, “Recent Visitors” or “Featured Members” tabs) on the members page.

    But lets go back to what I was trying to do. So I added a filter option into the dropdown to the others:

    ` // Function to add the “Only A” filter option
    function add_only_a_filter() {
    ?>
    <option value=”only_a”><?php _e( ‘Only A’, ‘buddypress’ ); ?></option>
    <?php
    }

    // Hook to add the “Only A” filter option to the Members directory order options
    add_action( ‘bp_members_directory_order_options’, ‘add_only_a_filter’ ); `

    The new filter option called “only A” shows up in the dropdown, so far so good. It is supposed to filter the results of any other search be it All Members, BP Search form results or even the Recent Visitor or Featured Member results to show only users whose name starts with an “A”.

    The filter shows up inside the dropdown but when I try to add a logic, it automatically filters alphabetically rather than showing me only those members whose name starts with an “A”.

     // Function to modify the members query based on the selected filter
    function modify_members_query_for_only_a( $query_args ) {
        if ( isset( $_GET['members_filter'] ) && $_GET['members_filter'] === 'only_a' ) {
            $query_args['field_id'] = '1'; // Adjust 'field_1' to the actual first name field key
            $query_args['meta_value'] = 'A%';
        }
        return $query_args;
    }
    
    // Hook to apply the filter to the members query
    add_filter( 'bp_after_has_members_parse_args', 'modify_members_query_for_only_a' ); 

    The field ID 1 is the First name field that I want to check who starts with “A”.
    I read in some of BuddyDevs older posts that its a good idea to hook into “‘bp_after_has_members_parse_args'” – which is what I was trying to do. I was hoping that by adding this filter logic the new filter option would show me users whose name starts with “A”.

    If this approach does not work, is there any way to add a separate tab maybe that might let me filter the results for other thiings than just alphabetical or recently active etc?

    Thanks again, Ravi 😉

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2934
    Ravi on #51678

    Hello Melaine,

    Thank you for the acknowledgement. I will update you by the weekend.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 6
    Melanie on #51681

    Hi Ravi, sure no worries 😉

    FYI, I tried a bit more things. I told you that I added the additional filter to the filter dropdown (recently active, newest and alphabetical) via: bp_members_directory_order_options

    I also tried to add it the same way as a tab instead like you add the tabs for “Featured Members” or “Recent Visitors” via: bp_members_directory_member_types

    All I want is this: have a new filter option to additionally filter the results. So lets say I check the Recent visitor or featured members tabs and see the results. They are not filtered yet, random, they show all members falling into the category (so either featured members or recent visitor based on what tab I selected).

    Now I want to filter them on top, to show for example only those whose name starts with an “A” as I previously mentioned. But this is where I am lost.

    I tried to hook into several options: bp_before_directory_members_content, bp_after_directory_members_content, bp_after_has_members_parse_args, bp_after_members_loop, etc but nothing worked.

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #51742

    Hi Melanie,
    Hope you are doing well.

    I am sorry that we could not assist you earlier. Ravi had assigned it to me but I missed.

    First of all, I would like to clarify that your use case has no relation to Featured members. Are you able to achieve it for default BuddyBoss members directory? I don’t believe that the above snippet would work.

    You can limit/filter using ‘xprofile_query’ for profile fields. It works similar to the meta_query args in WP_Query.(e.g you can pass multi dimensional array of field, comparison operators,

    The problem is BuddyBoss does not support xprofile_query in the bp_has_members() (BuddyPress does). So your code needs fine tuning at 2 places.
    1. We will hook to bp_core_get_users() and
    2. we will update your function to use xprofile_query

    Here is the updated code from your example(Your first part is fine for adding filter, It is the logic part).

    
    // Function to modify the members query based on the selected filter
    function modify_members_query_for_only_a( $query_args ) {
    
    	$type = $query_args['type'];
    	if ( $type === 'only_a' ) {
    		$query_args['xprofile_query'] = array(
    			array(
    				'field'   => '1',
    				'compare' => 'RLIKE',
    				'value'   => '^a'
    			)
    		);
    		//$query_args['field_id'] = '1'; // Adjust 'field_1' to the actual first name field key
    		//$query_args['meta_value'] = 'A%';
    	}
    
    	return $query_args;
    }
    
    // Hook to apply the filter to the members query
    //add_filter( 'bp_after_has_members_parse_args', 'modify_members_query_for_only_a' );
    add_filter( 'bp_after_core_get_users_parse_args', 'modify_members_query_for_only_a' );
    
    

    I have left the old parts to show you the change.

    Please give it a try and let me know if it works for you or not?

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved