1

I have a CPT called 'online-lots' for displaying items in an auction. The posts are grouped by custom taxonomy called 'sale' (e.g. '07_15 July Sale)', meaning that a whole catalogue of items can be viewed within each 'sale' custom taxonomy's archive page. In addition, each post is also allocated to a second custom taxonomy called 'room'. So an 'online lot' post is allocated to both a 'sale' and a 'room'.

When viewing the 'Sale' custom taxonomy archive page, it displays all the 'online lot' posts allocated to this sale. Now I wish to allow the user to filter these posts by 'Room' via a dropdown. So a user wishing to view all the items for sale in 'Room 2' would select the desired room from the dropdown and see the filtered results. I need this to occur via without a page refresh, as the whole taxonomy archive page is actually loaded into another part of the site via AJAX.

I have created the dropdown, populating options with the custom taxonomy 'room' values. I presume I need to modify the main query using the selected value from the dropdown (maybe introduce a tax_query?), but I'm not sure how to do this. Any advice would be appreciated -

How do I take the value from the dropdown and use it to filter the custom taxonomy archive posts?

Current code in functions.php:

function auction_custom_taxonomy_dropdown( $taxonomy ) {
    $terms = get_terms( $taxonomy );
    if ( $terms ) {
        echo '<form id="sale_selector" method="POST">
            <select id="sale_selection" name="' . $taxonomy . '" class="postform">
                <option value="">All Lots</option>';
                foreach ( $terms as $term ) {
                    if ( $_POST["room"] == $term->slug ){
                        $selected_option = 'selected';
                    } else {
                        $selected_option = '';
                    }
                    echo '<option '.$selected_option.' value="' . $term->slug . '">' . $term->name . '</option>';
                }
            echo '</select>
            <input class="filter_button" type="submit" value="OK">
        </form>';
    }
}

Then the function is called on the custom taxonomy archive page before the loop:

auction_custom_taxonomy_dropdown( 'room' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.