1
votes

I'm working on a site in which I need to query multiple terms in a taxonomy. Currently, I have the follow which works with drop-down menus.

function ct_search_form_select_quick($name, $taxonomy_name = null) {
    global $search_values;

    if (!$taxonomy_name) {
        $taxonomy_name = $name;
    }
    ?>
    <select id="ct_<?php echo $name; ?>" name="ct_<?php echo $name; ?>">
        <option value="0"><?php _e('Any', 'theme_textdomain'); ?></option>
        <?php foreach( get_terms($taxonomy_name, 'hide_empty=0') as $t ) : ?>
            <?php if ($search_values[$name] == $t->slug) { $selected = 'selected="selected" '; } else { $selected = ''; } ?>
            <option <?php echo $selected; ?>value="<?php echo $t->slug; ?>"><?php echo $t->name; ?></option>
        <?php endforeach; ?>
    </select>

However, I want to be able to query multiple terms in a single taxonomy.

I have the following code which allows me to picks terms and outputs a the results for the first term, but ignores the second term. The URL is output as

?property_type=condo-duplex&property_type=condo-loft&ct_price_from=&ct_price_to=&ct_mls=&property-search=true&x=32&y=18, which needs to be output as ?property_type=condo-duplex,condo-loft&ct_price_from=&ct_price_to=&ct_mls=&property-search=true&x=32&y=18

function ct_search_form_select($name, $taxonomy_name = null) {
    global $search_values;

    if (!$taxonomy_name) {
        $taxonomy_name = $name;
    }
    ?>
        <input type="checkbox" value="0" name="ct_<?php echo $name; ?>"><?php _e('Any', 'theme_textdomain'); ?>
        <?php foreach( get_terms($taxonomy_name, 'hide_empty=0') as $t ) : ?>-->
                    <?php if ($search_values[$name] == $t->slug) { $selected = 'selected="selected" '; } else { $selected = ''; } ?>

<input type="checkbox" name="<?php echo $name; ?>" value="<?php echo $t->slug; ?>"><?php echo $t->name; ?>
        <?php endforeach; ?>
2

2 Answers

0
votes

How convenient for you:

There is already an amazing plugin that achieves this exact functionality:

http://wordpress.org/extend/plugins/query-multiple-taxonomies/

See it in action here:

productionlocations.com/locations

0
votes

Not sure this is helpful a year later but I had a similar problem and this is what I came up with

function ct_search_form_check($name, $taxonomy_name = null) {
global $search_values;

    if (!$taxonomy_name) {
        $taxonomy_name = $name;
    }

?>
    <?php foreach( get_terms($taxonomy_name, 'hide_empty=0') as $t) : ?>
        <?php if ($search_values[$name] == $t->slug) { $selected = 'checked="checked"'; } else { $selected = ''; } ?>
            <div><input id="ct_<?php echo $name; ?>" name="ct_<?php echo $name; ?>" type="checkbox" style="margin-right:5px; margin-left:5px" <?php echo $selected; ?>value="<?php echo $t->slug; ?>"><?php echo $t->name; ?><span style="margin-left:10px"></span></input></div>
           <?php 
           $data = array();
                while (list($name, $t->slug) = each($arr)) {
                   $data[] = "$name";
                }
                echo implode($data); 
                if (!empty($_GET['ct_'])) { 
                    $url = str_replace('&ct_=', '%2c', $_SERVER['QUERY_STRING']);
                    header("Location: ?$url");
                } ?>
    <?php endforeach; ?>