Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Dynamic categories filtering
This topic contains 6 replies, has 2 voices, and was last updated by Ernest Marcinko 1 year, 3 months ago.
- AuthorPosts
- November 22, 2021 at 9:44 am #35667
Hello Support,
i have multiple search bar for each categorie with subcategories .
Could you tell me how to dynamically include new subcategories filters in each search bar when a user create new one?Thanks
Best regardsNovember 22, 2021 at 7:12 pm #35672Hi,
If you choose the “Use all from …” option, they will be included automatically on the filter: https://i.imgur.com/WYMU8mM.png
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
November 25, 2021 at 11:25 am #35732Hi Ernest,
thanks for your reply but this don’t resolve my question.
I have this situation :
Taxonomy Categories -> Parent Categories Test -> Child Category TestA , TestB
Taxonomy Categories -> Parent Categories Test2 -> Child Category TestC , TestDIn the filtering settings I’ve a checkbox for categories Test and all Child Category ( I don’t show categorie Test2 ) .
If I add a new Child Categories in Test , I cannot add them dynamically but I must go in settings page and adding the new Child category in the filtering .
I have multiple search instance with different categories and I would like to populate filtering checkbox automatically with newly create Child Categories.
I hope I explained better my need, sorry for bad English language but is not my primary language .
ThanksNovember 25, 2021 at 5:11 pm #35737In this case the only possible solution is to use a custom code – as there is no other way to force including all child terms.
I have constructed a small code snippet. All you need to edit is the $my_custom_fitlers array variable. I have included some explanation on how to use it.
For this to work, you must remove the existing filter, as the code will ad itt programmatically.add_action('asp_pre_parse_filters', 'asp_add_my_own_filters', 10, 2); function asp_add_my_own_filters($search_id, $options) { // This is the array of rules $my_custom_filters = array( array( 'search_id' => 1, // For which search ID should this filter exist 'taxonomy' => 'category', // Taxonomy name 'parent_id' => 25, // Parent term (category) ID for which include the child terms 'label' => 'Label', // The filter label 'type' => 'checkboxes', // The filter type 'selected' => true // Should the items be selected or not ), array( 'search_id' => 2, 'taxonomy' => 'category', 'parent_id' => 26, 'label' => 'Label', 'type' => 'checkboxes', 'selected' => true ) ); // --- DO NOT CHANGE ANYTHING BELOW ---- foreach( $my_custom_filters as $f ) { if ( $f['search_id'] == $search_id ) { // Creating a new filter $filter = wd_asp()->front_filters->create( 'taxonomy', $f['label'], $f['type'], array( 'taxonomy' => $f['taxonomy'] ) ); $terms = array( get_term_by('id', $f['parent_id'], $f['taxonomy']) ); $children = get_term_children($f['parent_id'], $f['taxonomy']); $terms = array_merge($terms, get_terms(array( 'hide_empty' => false, 'taxonomy' => $f['taxonomy'], 'include' => $children ))); $termsHierarchical = array(); wd_sort_terms_hierarchicaly($terms, $termsHierarchical); wd_flatten_hierarchical_terms($termsHierarchical, $terms); foreach( $terms as $term ) { // Add each taxonomy terms one by one $filter->add(array( 'id' => $term->term_id, 'label' => $term->name, 'level' => $term->level, 'taxonomy' => $f['taxonomy'], 'default' => false, 'parent' => $f['parent_id'], 'selected' => $f['selected'] )); } $filter->selectByOptions($options); wd_asp()->front_filters->add($filter); } } }
Try adding this code 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.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
November 25, 2021 at 5:35 pm #35740thank you Ernest, I’ll try and give you a feedback in the next days.
November 30, 2021 at 3:31 pm #35819Hello Ernest,
work like a charm !Thanks a lot
November 30, 2021 at 4:04 pm #35820You 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.