TransWikia.com

How to pass multiple values in a form for tax_query?

WordPress Development Asked by irishrunner16 on August 21, 2020

I am trying to create a form that allows users to view only specific posts. I have a custom taxonomy that has two parent categories, "Category" and "Age". The form has two filters for the child terms, all being categorized in the form with the same "name" value (name="typefilter").

The problem is, I can’t get the form to pass multiple values for the tax_query. Only a single value seems to be recognized. This leads to two undesirable results:

  1. When selecting "All" in either the Category section of the form OR the Age section of the form, only the first value is submitted to the query. For example, only value "36" is submitted if "All" is selected in the Age filter.
  2. When values are selected in both filters, such as value "35" in the Category, and value "37" in the Age filter, only the Age value is submitted.

Here is the code from my function:

    if( isset( $_POST['typefilter'] ) )
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'entry_categories',
                'field' => 'term_id',
                'terms' => $_POST['typefilter']
            )
        );

and this is the code from the form:

                    <form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST"
                        class="submissions-filter" id="filter">
                        <div class="filter-set submission-filter-category">
                            <h4> Category </h4>
                            <div class=" btn-group-toggle filter-buttons" data-toggle="buttons">
                                <label class="btn btn-primary">
                                    <input class="submissions-filter" type="radio" name="typefilter" value="34,35"/>
                                    All
                                </label>
                                <label class="btn btn-primary">
                                    <input class="submissions-filter" type="radio" name="typefilter" value="35"
                                        /> Individual
                                </label>
                                <label class="btn btn-primary">
                                    <input class="submissions-filter" type="radio" name="typefilter" value="34"
                                        /> Team
                                </label>
                            </div>
                        </div>
                        <div class="filter-set submission-filter-age">
                            <h4> Age </h4>
                            <div class="filter-buttons btn-group-toggle" data-toggle="buttons">
                            <label class="btn btn-primary order-first"> <input class="submissions-filter" type="radio" name="typefilter" value="36,37,38,39"/> All </label>
                            <label class="btn btn-primary"> <input class="submissions-filter" type="radio" name="typefilter" value="36"/> 5-7 </label>
                            <label class="btn btn-primary"> <input class="submissions-filter" type="radio" name="typefilter" value="37"/> 8-10 </label>
                            <label class="btn btn-primary"> <input class="submissions-filter" type="radio" name="typefilter" value="38"/> 11-13 </label>
                            <label class="btn btn-primary"> <input class="submissions-filter" type="radio" name="typefilter" value="39"/> 14-21 </label>
                            </div>
                        </div>
                        <div class="filter-dropdown submission-filter-country">
                            <label for="country-filter">
                                <h4>Country </h4>
                            </label>
                            <select name="countryfilter" id="country-filter">
                                <option name="countryfilter" value="Colombia, Mexico, USA, United States">All</option>
                                <option name="countryfilter" value="Colombia"> Colombia </option>
                                <option name="countryfilter" value="Mexico"> Mexico </option>
                                <option name="countryfilter" value="USA, United States"> USA </option>
                            </select>
                        </div>
                        <div class="filter-dropdown submission-filter-sortby">
                            <label for="country">
                                <h4>Sort By </h4>
                            </label>
                            <select name="" id="filter-options">
                                <option value="">Submission Name</option>
                                <option value=""> Student Name </option>
                                <option value=""> Student Age </option>
                            </select>
                        </div>
                        <button class="filter-button btn-secondary go-button">Go</button>
                        <input type="hidden" name="action" value="myfilter">
                    </form>

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP