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 3 years, 4 months ago.
- AuthorPosts
- July 22, 2020 at 1:08 pm #28607
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
July 22, 2020 at 1:31 pm #28608Hi,
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:
Best,// 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; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
July 22, 2020 at 2:07 pm #28609Hi
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 ?
July 22, 2020 at 4:06 pm #28616Well, 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. SaveNow, 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 :)
July 23, 2020 at 7:40 am #28631Hi,
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.
July 23, 2020 at 8:19 am #28634Hi,
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 :)
July 23, 2020 at 10:29 am #28641Hi
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.
July 23, 2020 at 12:34 pm #28642You cannot access this content. Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
You must be logged in to reply to this topic.