0

I Need to build an interface with a filter set (a list of checkboxes) that the user can check and click 'filter' to search with in 1 custom post-type for posts with matching terms (there are 3 custom taxonomies with each an unlimited amount of terms).

How to go about this? I am thinking to code the following:

I displayed the GUI filterlist like this (x3 because there are 3 taxonomies):

<form action="/filter-result" method="get">
    <?php $terms = get_terms( array(
        'taxonomy' => 'taxonomy-name',
        'hide_empty' => false,
    ) );
    foreach($terms as $term) {
         echo '<label><input type="checkbox" name="taxonomy-name" value="' . $term->name .'">' . $term->name . '</label>';
    } ?>
    <input type="submit" value="Filter">
</form>

I end up on the /filter-result page with an URL with more than one parameter that all have the same name: filter-result/?taxonomya=term1&taxonomya=term2&taxonomyb=term9

My plan is to GET all the parameters- I don't know how because there are parameters with the same name- and then build an SQL out of the filtered data.

1

Add brackets to the input names:

<label><input type="checkbox" name="taxonomy-name[]" value="term">term</label>

It will be automatically converted to an array:

if( isset( $_GET['taxonomy-name'] ) ){
    foreach( $_GET['taxonomy-name'] as $term ){
        echo $term;
    }
}
0

As an addition to Milo's answer for the sake of completeness, this is how I formed the args for the sql using the URL parameters:

$query = new WP_Query( array(
    'posts_per_page' => -1,
    'post_type' => 'mycustomposttype',
    'orderby' => 'title',
    'order'   => 'ASC',
    'tax_query' => array(
        'relationship' => 'AND',
        array(
            'taxonomy' => 'mytaxonomy1',
            'field' => 'slug',
            'terms' => $_GET['mytaxonomy1'],
            'operator' => 'AND',
        ),
        array(
            'taxonomy' => 'mytaxonomy2',
            'field' => 'slug',
            'terms' => $_GET['mytaxonomy2'],
            'operator' => 'AND',
        ),
        array(
            'taxonomy' => 'mytaxonomy3',
            'field' => 'slug',
            'terms' => $_GET['mytaxonomy3'],
            'operator' => 'AND',
        ),
    ),
) );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
    }
}

See for more elaboration this question. I also found this a great resource for making the WP_Query: WP_Query Arguments: Taxonomies

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.