• Hello,
    I wonder if it would be possible to set 2 or 3 different instances of the search form with specific setting for each ones. For example :
    A search form dedicated to member’s search,
    A search form dedicated to group’s search,
    A search form dedicated to a sport type search,
    etc…

    This features would be awesome if you wanted to have a website with a live search dedicated to some specific templates in your theme!

    Thanks for your answer ??

    https://www.ads-software.com/plugins/buddypress-global-search/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter Pierre from DesignBots

    (@mecanographik)

    Maybe i could add a filter in my functions.php to filter search types based on the current page?
    Ex: include only ‘groups’ search type if i’m on the bp groups directory page, for eg. ?
    Which filter could i use?
    Thanks for your help

    Thread Starter Pierre from DesignBots

    (@mecanographik)

    Hi
    I’ve tried to use this filter : buddyboss_global_search_option_items-to-search
    but it only worked on search result page but didn’t work on ajax auto-completion search results.

    Here is my function:

    function bboss_global_search_remove_search_types( $search_types ){
      if( !is_admin() && !empty( $search_types ) ){
        $items_to_remove = array( 'posts' ); // test excluding 'posts' search type
    
        if ( is_search() ) {
        $items_to_remove = array( 'members' ); // working only on search result page
        }
    
        $filtered_search_types = array();
        foreach( $search_types as $search_type ){
          if( !in_array( $search_type, $items_to_remove ) ){
            $filtered_search_types[] = $search_type;
          }
        }
    
        $search_types = $filtered_search_types;
      }
    
      /*
      echo '<pre>';
      print_r($search_types);
      echo '</pre>';
      */
    
      return $search_types;
    }
    add_filter( 'buddyboss_global_search_option_items-to-search', 'bboss_global_search_remove_search_types', 1 );

    Please help me to make it working on auto-completion search results component.

    Regards,

    Thread Starter Pierre from DesignBots

    (@mecanographik)

    Heads up:
    The Buddyboss Team has kindly reply to my ticket within the official Buddyboss support section, so here is the working snippet in case someone need it!

    /*
    * custom code for Buddypress Global Search
    * filter search types
    * We override bb_global_search_default_items_to_search()
    * see: includes/class.BBoss_Global_Search_Helper.php
    */
    
    function bboss_global_search_filter_search_types( $search_types ){
    
      if( !empty( $search_types ) ){
    
        //$items_to_remove = array( 'posts' ); // test excluding only this search type
        $items_to_keep = array( 'posts' ); // test including only 'posts' search type for eg.
    
        if ( is_search() ) {
          $items_to_keep = array( 'members' ); // test: applied only on search results page
        }
    
        $filtered_search_types = array();
        foreach( $search_types as $search_type ){
          // if( !in_array( $search_type, $items_to_remove ) ){
          if( in_array( $search_type, $items_to_keep ) ){
            $filtered_search_types[] = $search_type;
          }
        }
        $search_types = $filtered_search_types;
    
        /*
        // for debug
        echo '<pre>';
        print_r($search_types);
        echo '</pre>';
        */
    
        return $search_types;
      }
    }
    remove_filter( 'buddyboss_global_search_option_items-to-search', 'bb_global_search_default_items_to_search' );
    add_filter( 'buddyboss_global_search_option_items-to-search', 'bboss_global_search_filter_search_types');

    It works great on auto completion, thanks a lot BB Team!

    Plugin Author BuddyBoss

    (@buddyboss)

    Thanks mecanographik

    Hi,

    Was hoping for some insight here, as I’m looking for the exact same thing.

    I can get this general function working in functions.php, but not based on current page, since the buddyboss_global_search_option_items-to-search fires way before any of the queries are fired.

    So I’m curious as to how you got this working conditionally for different templates/pages.

    Thanks in advance!

    So this is where I’m going insane…

    The code you listed above, when placed in functions.php fires twice for some reason. I’m not sure why. The same thing happens with my code below. It seems to be due to the buddyboss_global_search_option_items-to-search hook.

    Here’s the only way I could get the current page URL given that this fires so early.

    In functions.php:

    
    // Set search topics by page
    function mpc_bboss_global_search_filter_search_types($search_types){
    
    	// get page path
    	$uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    	$uri_segments = explode('/', $uri_path);
    	$currpage = $uri_segments[1];
    
    	// filter search options based on page
      	if(!empty($search_types )){
    
    		if ($currpage == "discussions"){
        			$items_to_keep = array('topic');
    		}
    		else if ($currpage == "company-groups"){
        			$items_to_keep = array('forum');
    		}
    		else {
        			$items_to_keep = array('topic','forum');
    		}
    
        		$filtered_search_types = array();
        		foreach( $search_types as $search_type ){
          			if(in_array($search_type, $items_to_keep)){
            			$filtered_search_types[] = $search_type;
          			}
        		}
        		$search_types = $filtered_search_types;
    		//var_dump($search_types);
    
        		return $search_types;
      	}
    
    }
    remove_filter('buddyboss_global_search_option_items-to-search', 'bb_global_search_default_items_to_search');
    add_filter('buddyboss_global_search_option_items-to-search', 'mpc_bboss_global_search_filter_search_types');
    

    If i place a var_dump or echo in any of the $currpage statements, I can see they’re working properly according to the page URL I’m on. Additionally, firing the var_dump of $search_types at the bottom outputs the correct array.

    That said, the correct search types never actually seem to get set to the search.

    For some reason they always take the default else statement $items_to_keep = array(‘topic’,’forum’); Essentially, it ONLY works if they are not contained within the case statement.

    I’m losing my mind on this. I don’t get why the function is firing 2-3 times and not setting despite everything else telling me that it is setting.

    • This reply was modified 8 years, 1 month ago by ilovemetrics.
    Plugin Author BuddyBoss

    (@buddyboss)

    @ilovemetrics

    You would need this custom code to remove the global search plugin filter to avoid 2-3 times calls, and remove_filter function should be inside init hook and priority must be 21.

    function remove_option_items_to_search() {

    remove_filter(‘buddyboss_global_search_option_items-to-search’, ‘bb_global_search_default_items_to_search’);
    }

    add_action( ‘init’, ‘remove_option_items_to_search’, 21 );

    ilovemetrics

    (@ilovemetrics)

    Hi,

    Thank you for the response. Sorry, but I’m not following. I have no problem removing the default settings with the code above. The problem is when it’s re-set in the mpc_bboss_global_search_filter_search_types function.

    The main issue is that I’d like to set the search options based on the page URL, but I’m unable to actually set anything in the code above. The case statements all work correctly and my last “return $search_types” line is returning the correct types, but when I load the page, it’s still searching everything. I’m confused, as all the logic is working correctly, it’s just not being set for some reason.

    Thanks

    Plugin Author BuddyBoss

    (@buddyboss)

    Hi @ilovemetrics

    Try to replace

    add_filter('buddyboss_global_search_option_items-to-search', 'mpc_bboss_global_search_filter_search_types');

    with this

    add_filter('buddyboss_global_search_option_items-to-search', 'mpc_bboss_global_search_filter_search_types', 21);

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Multiple instance of search form with different settings’ is closed to new replies.