forms - WordPress Multiple Taxonomy Query

admin2025-01-07  5

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:

/?country=ireland&_type=partner

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

/?country=ireland&country=united-kingdom&_type=partner

My question is, how can I use this form to create a conditional 'OR' request more like /?country=ireland,united-kingdom&type=partner?

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=ireland&country=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-kingdom&type=partner?

Share Improve this question edited Nov 3, 2016 at 12:16 Tom J Nowell 60.7k7 gold badges77 silver badges147 bronze badges asked Nov 3, 2016 at 12:07 Kevin NugentKevin Nugent 5631 gold badge8 silver badges20 bronze badges 2
  • The answer to this will need to involve term IDs rather than term slugs, as the parameters in the URL map on to WP_Query, and WP_Query can't accept more than one slug for a taxonomy query, but it can accept multiple term IDs with a different parameter. There'll also need to be a change to the form inputs, likely country will become country[] etc – Tom J Nowell Commented Nov 3, 2016 at 12:14
  • I've just tested some variations with an OR (comma) such as "country=ireland,united-kingdom" and they return the correct results. – Kevin Nugent Commented Nov 3, 2016 at 12:22
Add a comment  | 

1 Answer 1

Reset to default 0

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().

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736261456a743.html

最新回复(0)