$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom taxonomy - Build filter for post-type + multi taxonomies + multi terms|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom taxonomy - Build filter for post-type + multi taxonomies + multi terms

matteradmin10PV0评论

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.

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.

Share Improve this question asked Jan 19, 2019 at 20:15 LudoLudo 612 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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;
    }
}

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

Post a comment

comment list (0)

  1. No comments so far