Add condition to "asp_query_args"

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Add condition to "asp_query_args"

This topic contains 8 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 4 years, 1 month ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #25587
    maxsstyle
    maxsstyle
    Participant

    Hi Ernesto and thank you very much for this very good search engine.

    In order to increase results’ quality I use the “asp_query_args” filter. I’m using a condition in order to avoid the filter to work on the search-form number 13 (this is a specific buddypress form).

    I’m currently adding a new form (number 14) to my website with results which are both coming from the blog part and the buddypress part. Therefore I would like to add another condition so that the buddypress results are not filtered in this new form.

    Something like this :

    If ( results != buddypress result ) { do this
    } else { do that
    } ;

    Here is my current code :

    add_filter('asp_query_args', 'map_search_phrase', 1, 1);
    function map_search_phrase($args) {
      $search_syn = $_GET["p_asid"];
        global $wp_query;
          if ( $search_syn != '13'  ) {
        // Add or remove items to this variable
            $args['s'] = mb_strtolower($args['s']);
            $mapping = array(
            "calculs" => "calcul",
            "rénaux" => "rénal",
            "rénale" => "rénal",
            "génitales" => "génitale",
            "génital" => "génitale",
            "génnitales" => "génitale",
            );
        }
        // ----------------------------------
        // -- DO NOT CHANGE ANYTHING BELOW --
          $initial = array();
          $replace = array();
          foreach ($mapping as $k => $v) {
              $initial[] = "/\b".preg_quote($k)."\b/u";
              $replace[] = $v;
          }
          $args['s'] = preg_replace($initial, $replace, $args['s']);
          return $args;
    }

    Thank you very much,
    Please feel free to ask me more questions if you need more informations,
    Best regards,

    Nicolas

    #25589
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Nicolas,

    I made an adjustmend to your code, now you can enter the search IDs to the $exclude_ids array, to which you don’t want the code to execute:

    add_filter('asp_query_args', 'map_search_phrase', 10, 2);
    function map_search_phrase($args, $search_id) {
    	// search IDs to exclude
    	$exclude_ids = array(13, 14);
    	
    	if ( !in_array($search_id, $exclude_ids) ) {
    		$args['s'] = mb_strtolower($args['s']);
    		$mapping = array(
    			"calculs" => "calcul",
    			"rénaux" => "rénal",
    			"rénale" => "rénal",
    			"génitales" => "génitale",
    			"génital" => "génitale",
    			"génnitales" => "génitale",
    		);
    		$initial = array();
    		$replace = array();
    		foreach ($mapping as $k => $v) {
    			$initial[] = "/\b".preg_quote($k)."\b/u";
    			$replace[] = $v;
    		}
    		$args['s'] = preg_replace($initial, $replace, $args['s']);
    	}
    	return $args;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #25592
    maxsstyle
    maxsstyle
    Participant

    Thank you Ernesto.

    I probably wasn’t clear enough. My previous code worked and I just tried yours and he works too.

    But this is not what I wanted to do. I don’t want to exclude the entire form number 14 from the filter.

    In my form number 14 I am searching for custom post-type in wordpress AND buddypress activities in buddypress. If we imagine that on 100 results, 20 are from buddypress, I wan’t to exclude the 20 from the filter, but the 80 others should be included in the filter.

    add_filter('asp_query_args', 'map_search_phrase', 10, 2);
    function map_search_phrase($args, $search_id) {
    	// search IDs to exclude
    	$exclude_ids = array(13, 14);
    	
    	if (  !in_array($search_id, $exclude_ids AND results != buddypress_results ) ) {
    		$args['s'] = mb_strtolower($args['s']);
    ...

    Thank you again,
    Best regards,

    Nicolas

    #25593
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    I don’t think that is possible, as the filtr executes before the searhc is completed, and passes the query arguments. I’m afraid it is not possible to modify the phrase only for the buddypress results, you need a separate search form only for that. The change will apply to the whole of the form only.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #25606
    maxsstyle
    maxsstyle
    Participant

    Ok thank you again for your answer.

    So would it be possible to have two search form with the results displaying on two different loops on the same page ?

    I could hide the second form and use Javascript to copy the search phrase in the second form.
    So when when the user write a search phrase and press enter :
    Form 1 would have the results displayed in loop 1.
    Form 2 would have the results displayed in loop 2.

    If it’s possible could you please explain me how ?

    Thank you,
    Best regards,

    Nicolas

    #25627
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Nicolas,

    I am not sure if that is possible without decent custom coding. It sounds possible, but it will definitely require some advanced coding.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #25628
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Nicolas,

    I am not sure if that is possible without decent custom coding. It sounds possible, but it will definitely require some advanced coding.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #25712
    maxsstyle
    maxsstyle
    Participant

    Hi,

    I finally managed to do what I wanted using an iframe with the results of a search form dedicated to buddypress. Now I would like to order the results using a custom field but the buddypress’ custom-fields doesn’t seems to be found. Ordering with custom-fields only works for post and post-type.

    Is it normal ?

    Thank you,
    Best regards,

    Nicolas

    #25725
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Nicolas,

    Buddypress content (groups, activities) are not regular custom post types, so custom fields for them does not exist as post meta. Unfortunately that won’t work – even if you entered the field names manually, as those fields does not exists in the custom fields table.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.