How to show search results for a category in a specific template

Home Forums Product Support Forums Ajax Search Pro for WordPress Support How to show search results for a category in a specific template

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

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #28607
    uniconta18
    uniconta18
    Participant

    Hi

    Before using this plugin, I used WordPress native search in which I added a parameter ‘cat’ in the search form for a specific category. In my functions.php I then redirected the template to that specific category page. So, when I searched in that category I always stayed in that category page template.

    Now, I created this new search from your plugin, I also included that specific category to search in.. but unfortunately I am not able to get it redirected to that category template.

    I hope you understand my point. For the search in a category, I need to change the template instead of using the default search result page.

    Thanks

    #28608
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Depending on the situation, I recommend two possible solutions:

    1. You can set the search to redirect to a custom URL, where you can add a static parameter, like ?s={phrase}&cat=1

    2. To use the redirection to custom URL, and add the parameters conditionally via custom code based on this example. In your case, something like this:

    // Use this to change the "redirect to url" parameter
    add_filter( 'asp_redirect_url', 'asp_add_params_to_url', 1, 10 );
    // Use this to change the "show more results url" parameter
    add_filter( 'asp_show_more_url', 'asp_add_params_to_url', 1, 10 );
    
    function asp_add_params_to_url( $url ) {
      // Array of param names and values
      $values = array(
        "cat" => "1"
      );
      // Merge them together
      foreach ( $values as $k => $v )
        $url .= "&".$k."=".$v;
    
      return $url;
    }
    Best,
    Ernest Marcinko

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


    #28609
    uniconta18
    uniconta18
    Participant

    Hi

    Thanks for your quick response.

    Since I have multiple search bars & category cannot be static, is there a way in this function I can find which search bar id it is ?

    #28616
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Well, the problem is, that the hook does not pass the search ID. However making a change to one of the core files will fix that as well:
    1. Open up the wp-content/plugins/ajax-search-pro/includes/views/asp.shortcode.script.php file on your server
    2. Replace it’s contents with this
    3. Save

    Now, the filter accepts a second parameter, which is the search ID:

    // Use this to change the "redirect to url" parameter
    add_filter( 'asp_redirect_url', 'asp_add_params_to_url', 2, 10 );
    // Use this to change the "show more results url" parameter
    add_filter( 'asp_show_more_url', 'asp_add_params_to_url', 2, 10 );
    
    function asp_add_params_to_url( $url, $id ) {
      // Array of param names and values
      $values = array(
        "cat" => "1"
      );
      // Merge them together
      foreach ( $values as $k => $v )
        $url .= "&".$k."=".$v;
    
      return $url;
    }

    I will include this change in the upcoming release, so you don’t have to worry about the additional parameter.

    Best,
    Ernest Marcinko

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


    #28631
    uniconta18
    uniconta18
    Participant

    Hi,

    Thats great, thanks 🙂

    But now I am facing another issue. I have 2 search bars on the same page. One in the top bar & second inside the page.

    So, when I tried your code, the id I am receiving is of the topbar search and not the one I am using that is from the page.

    #28634
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    You are welcome 🙂

    Can you share the custom code you are using there? The ID seems to be correctly passed on our test servers.

    Best,
    Ernest Marcinko

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


    #28641
    uniconta18
    uniconta18
    Participant

    Hi

    Thanks it worked. Actually at the back-end I had set the action to go to results page instead of custom url and because of which it was redirecting to the page where this search bar wasn’t present and that’s why the id came out wrong.

    Finally it’s sorted.

    Thanks alot for your quick help. Really appreciated.

    #28642
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

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


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

You must be logged in to reply to this topic.