Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Category wise search
- This topic has 2 replies, 2 voices, and was last updated 1 year, 2 months ago by
misba.
-
AuthorPosts
-
March 7, 2025 at 5:52 pm #53222
misba
ParticipantHi,
I want to create a separate search bar for each page (it has product category shortcode). Example: this page is an aviation category page so the search bar should only automatically show results from the aviation page. Similar use cases I want to apply to each category page for a separate search bar where the result are only based on the particular category. Also it should only show me search results only for aviation product currently when I type certain keywords the results appearing for rest category as well how can i restrict the results in search just for specific categoryKindly note: The main page (Products) has all the products and that search is open to all categories.
-
This topic was modified 1 year, 2 months ago by
misba.
March 8, 2025 at 9:32 am #53227Ernest Marcinko
KeymasterHi,
It is possible to restrict the results to the current category of the page via this custom code snippet.
However in your case it may be a bit more complicated as using the category shortcode does not indicate which category the page is in – they are not connected in any way. To do that, you would have to create a programmatical map (array) of pages and product categories it should restrict. I’m thinking using this example as the base.
Here is a super simple custom code that should do the trick. You want to change the $page_rules variable, you can add as many lines as you want. On the left side is the page ID, on the right side is the category ID to which you want to restrict the search results to on that given page.
add_filter( 'asp_query_args', 'asp_include_only_term_ids', 10, 2 ); function asp_include_only_term_ids( $args, $id ) { /** * Format: page_id => product category it should be restricted to * For example, if you want to restrict page ID 123 to category ID 5: 123 => 5, * You can add as many lines as you want */ $page_rules = array( 123 => '5', 456 => '6', ); // -- !! Do not change anything below this line !! -- if ( !is_array($args['post_tax_filter']) ) { $args['post_tax_filter'] = array(); } foreach ( $page_rules as $page_id => $term_string ) { if ( $page_id !== intval($args['_page_id']) ) { continue; } $terms = explode(',', $term_string); foreach ( $terms as $tk => &$tv ) { $tv = trim($tv); } $args['post_tax_filter'][] = array( 'taxonomy' => 'product_cat', 'include' => $terms, 'allow_empty' => false, ); } return $args; }I’m more than happy to help, but please note that this level of customization is beyond support, so I can’t guarantee anything. If you want professional customizations, I recommend our affiliates at wpkraken.
Try adding this code via the Code Snippets plugin or to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.
April 4, 2025 at 4:53 am #53584misba
ParticipantHi,
I have managed to find an easier way around this for noncoders like me. Check it out for if anyone ever needs any help: https://labcon.in/aviation/ -
This topic was modified 1 year, 2 months ago by
-
AuthorPosts
- You must be logged in to reply to this topic.