Dynamic categories filtering

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 Ernest Marcinko 2 years, 4 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #35667
    ictcustomer28
    ictcustomer28
    Participant

    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 regards

    #35672
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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 :)


    #35732
    ictcustomer28
    ictcustomer28
    Participant

    Hi 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 , TestD

    In 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 .
    Thanks

    #35737
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    In 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 :)


    #35740
    ictcustomer28
    ictcustomer28
    Participant

    thank you Ernest, I’ll try and give you a feedback in the next days.

    #35819
    ictcustomer28
    ictcustomer28
    Participant

    Hello Ernest,
    work like a charm !

    Thanks a lot

    #35820
    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 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.