How to access search phrase via add_filter

Home Forums Product Support Forums Ajax Search Pro for WordPress Support How to access search phrase via add_filter

This topic contains 3 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 7 years, 7 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #10107
    luyendao
    luyendao
    Participant

    Hi Ernest, thank you for your excellent support as always.

    I searched thoroughly through the KB, but could not find the answer to this. I need to access $s – the search phrase in two places.

    1)
    One is above the search results, in which case I added this:

    add_filter('asp_layout_before_results','asp_add_header_data', 1,2);
    function asp_add_header_data ($results) {
    	var_dump($_GET);
    	echo '<h3>Results for clinics near ...</h3>';
    }

    But could not find the search phrase data, when i dumped $results and $_GET i get an empty array back. Do I need to specify the search instance? I’m not sure how or what object is available when I use that filter.

    2)
    The other place I was going to use the search phrase was to check on each individual search item for a match, but maybe you have a better idea how to solve this.

    My search instance searches Locations – and if the search phrase matches the post title, i want to set a variable to flag this on an individual result, and of course do the opposite if it does not. The reason I’m doing it this way, is there’s an array of keywords in a custom field for each location post type I have of nearby cities – it acts like a fuzzy search. It’s a dirty way of doing things, but i avoid having to run multiple queries against a geo API as I don’t need additional data like distance etc…

    I think if you can help me answer #1, then I can solve #2 as well.

    Thanks again for your time,
    Luyen

    #10123
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Luyen,

    To access the search phrase, use the asp_search_phrase_before_cleaning or the asp_search_phrase_after_cleaning filters.

    The search data is passed via a POST request. Within this context you should be able to access the raw $_POST data as well:

    add_filter('asp_search_phrase_before_cleaning', 'asp_change_search_phrase ', 1,1);
    
    function asp_change_search_phrase ($phrase) {
        var_dump($_POST);
        return $phrase;
    }
    Best,
    Ernest Marcinko

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


    #10136
    luyendao
    luyendao
    Participant

    Hi Ernest,

    Thanks that’s helpful to know how to modify the search phrase.

    I’m not sure how to output the search phrase above the results in the first place – so it outputs “Your search for search_phrase” above the results. I was thinking if I can access the function that returns the $phrase, then output it into the hook ‘asp_add_header_data’ that should work?

    When i added the code above on its own, i get nothing returned and the whole results array comes back empty.

    I can pass a test string into asp_change_search_phrase() and get that back, but can’t seem to access $phrase itself.

    I am running multiple search instances – any ideas?

    Thanks again.

    • This reply was modified 7 years, 7 months ago by luyendao luyendao.
    #10165
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    Sorry for the late response, I messed up my forum notifications and missed a few tickets.

    Well, I’ve looked into this in more detail and the problem is that there is no action/filter executed before printing the results list. Even bigger problem is that the data printed is delimited between two strings to avoid any error messages to appear in the results list in case something else is malfunctioning.

    The only solution is to make a tiny modification in the search code and add then use a filter.

    1. Open up the wp-content\plugins\ajax-search-pro\includes\classes\ajax\class-asp-search.php file and scroll to lines 87-89:

    if (isset($_POST['asp_get_as_array'])) {
        return $results;
    }

    ..just below these 3 lines add this extra line:

    $html_results = apply_filters('asp_before_ajax_output', $html_results);

    This will enable access to the final HTML results list.

    2. Now, use this filter to print extra information to the ajax response:

    add_filter('asp_before_ajax_output', 'asp_print_before_first_result', 1);
     
    function asp_print_before_first_result( $html ) {
        $search_id = $_POST['asid'];
        $phrase = $_POST['aspp'];
        $ids = array(1, 2, 3); // The search IDs you want to use this code on
        
        if ( in_array($search_id, $ids)) {
          $html = "<div class='asp_the_phrase'>Results for: ".esc_sql($phrase)."<div>" . $html;
        }
        
        return $html;
    }

    Don’t forget to change the $ids array to the ids you want the code to apply on.

    This appears to be working on my test environment, but let me know.

    Best,
    Ernest Marcinko

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


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

You must be logged in to reply to this topic.