• Hi,
    I would like to create a second category filer (dropdown) so a user can filter by for example ‘Store Type’ and ‘Store Size’. I have 2 parent categories set up ie Type and Size and they each have the relevant child categories. I am successfully populating the normal category filter dropdown with just the child categories of Type using this

    add_filter( 'wpsl_dropdown_category_args', 'nm_custom_dropdown_category_args' );
    function nm_custom_dropdown_category_args( $args ) {
    
        if ( is_page( 'home' ) ) {
            $args['parent'] = 6; // parent category ID of Type
            $args['hierarchical'] = true; // only show the child categories of Type
        }
        return $args;
    }

    I guess I could create secondary create_category_filter() and set_selected_category() functions but hoping their might be a better way?

    Many thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Tijmen Smit

    (@tijmensmit)

    If you copy the code from line 1423 till 1444 and place it in a new function in the functions.php that you then call in a custom template.

    Do make sure to include the ‘wpsl-custom-dropdown’ class in the $args and use a different ID. The ‘wpsl-custom-dropdown’ class makes sure the selected values are automatically included in the submitted AJAX data.

    You will also have to modify the SQL query and place holders to do something with the submitted data.

    Thread Starter southafricanrob

    (@southafricanrob)

    Hi Tjimen,
    Thanks for the quick reply and suggestions. I have created this function in my themes functions.php

    //add a second category dropdown in the search filters
    function nm_add_second_category_filter(){
        $category = '<div id="wpsl-loyalty">' . "\r\n";
            $category .= '<label for="wpsl-loyalty-list">Loyalty Program</label>' . "\r\n";
    
            $args = apply_filters( 'wpsl_dropdown_category_args', array(
                    'show_option_none'  => 'Any',
                    'option_none_value' => '0',
                    'orderby'           => 'NAME',
                    'order'             => 'ASC',
                    'echo'              => 0,
                    'selected'          => $this->set_selected_category( $filter_type ),
                    'hierarchical'      => 1,
                    'name'              => 'wpsl-loyalty',
                    'id'                => 'wpsl-loyalty-list',
                    'class'             => 'wpsl-dropdown wpsl-custom-dropdown',
                    'taxonomy'          => 'wpsl_store_category',
                    'hide_if_empty'     => true
                )
            );
    
            $category .= wp_dropdown_categories( $args );
    	
        $category .= '</div>' . "\r\n";
        
    }

    but am coming a little unstuck trying to include it in my custom template. Currently I am doing this but is stooping the page from loading.

    if ( $this->use_category_filter() ) {
                $output .= $this->create_category_filter();
                //include the second category filter
                $output .= nm_add_second_category_filter();
            }

    Also, can you advise why I would need to modify the sql query as it is still just a (additional) category that is being filtered?
    Many thanks,

    Rob

    Plugin Author Tijmen Smit

    (@tijmensmit)

    Currently I am doing this but is stooping the page from loading.

    Can you check the server error log, it will likely explain the problem.

    Also, can you advise why I would need to modify the sql query as it is still just a (additional) category that is being filtered?

    The default category filter will be passed in the AJAX data in the filter param, additional ( custom ) dropdowns will not be assigned to the same key / value pair. The key of the custom dropdowns is based on the name value, and this will by default not be processed in the SQL query, unless you tell it to.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Create second category filter’ is closed to new replies.