WordPress Multiple Taxonomy Query

I've a simple form made up of two multiple selects which (as if by magic) filters my posts archive with URL parameters:

form class="filters"
    select name="country" multiple
        option value="united-kingdom"United Kingdom/option
        option value="ireland"Ireland/option
    /select
    select name="type" multiple
        option value="director"Director/option
        option value="partner"Partner/option
    /select
    input type="submit"
/form

If I select 'Ireland' and 'Partner' I'm sent to:

https://example.com/network/?country=ireland_type=partner

Then, if I select multiple values such as 'Ireland', 'United Kingdom' and 'Partner' I'm sent to:

https://example.com/network/?country=irelandcountry=united-kingdom_type=partner

My question is, how can I use this form to create a conditional 'OR' request more like https://example.com/network/?country=ireland,united-kingdomtype=partner?

Topic tax-query select custom-taxonomy forms Wordpress

Category Web


Perhaps you can add a hidden field to the HTML which will pass the 'relation' => 'OR'.

<input type="hidden" name="tax_query[relation]" value="OR">

<select name="tax_query[][country]" multiple>
    <option value="united-kingdom">United Kingdom</option>
    <option value="ireland">Ireland</option>
</select>

<select name="tax_query[][type]" multiple>
    <option value="director">Director</option>
    <option value="partner">Partner</option>
</select>

Which translates to:

tax_query[relation]=OR&tax_query[][country]=united-kingdom&tax_query[][type]=director

Array
(
    [tax_query] => Array
        (
            [relation] => OR
            [0] => Array
                (
                    [country] => united-kingdom
                )

            [1] => Array
                (
                    [type] => director
                )

        )

)

Seems like your network page is custom and would find that with $_REQUEST.


Just referencing how to handle multiple taxonomy queries and modeling based on http_build_query() to add 'relation' => 'OR'.

$args = http_build_query( array (

    'post_type' => 'post',

    'tax_query' => array (

        'relation' => 'OR',

        array (
            'taxonomy' => 'movie_genre',
            'field'    => 'slug',
            'terms'    => array ( 'action', 'comedy' ),
        ),

        array (
            'taxonomy' => 'actor',
            'field'    => 'term_id',
            'terms'    => array ( 103, 115, 206 ),
            'operator' => 'NOT IN',
        ),
    ),
) );

echo "<pre>";

echo PHP_EOL . $args . PHP_EOL;

print_r( wp_parse_args( $args ) );

Which ends up looking like:

post_type=post&tax_query%5Brelation%5D=AND&tax_query%5B0%5D%5Btaxonomy%5D=movie_genre&tax_query%5B0%5D%5Bfield%5D=slug&tax_query%5B0%5D%5Bterms%5D%5B0%5D=action&tax_query%5B0%5D%5Bterms%5D%5B1%5D=comedy&tax_query%5B1%5D%5Btaxonomy%5D=actor&tax_query%5B1%5D%5Bfield%5D=term_id&tax_query%5B1%5D%5Bterms%5D%5B0%5D=103&tax_query%5B1%5D%5Bterms%5D%5B1%5D=115&tax_query%5B1%5D%5Bterms%5D%5B2%5D=206&tax_query%5B1%5D%5Boperator%5D=NOT+IN

And can be parsed by wp_parse_args().

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.