1

I'm trying to create a search feature that will allow users to enter a search term and select a couple of categories. The script searches a single category just fine, but when I add more than one I hit a roadblock.

My code is as follows:

<form method="get" id="searchform" action="<?php echo home_url(); ?>">
    <input type="text" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Search...':this.value;" value="Search..." name="s" id="s" />
    <input type="hidden" name="post_type" value="product" />
    <?php wp_dropdown_categories('taxonomy=product_cat&id=make&child_of=15&show_option_all=Select Make...'); ?>
    <?php wp_dropdown_categories('taxonomy=product_cat&id=model&child_of=21&show_option_all=Select Model...'); ?>
    <input type="submit" id="searchsubmit" value="Search" />
</form>

As I was searching around I discovered: Wordpress Multiple Category Search, which seems to do exactly what I want...however I'm not sure how to incorporate the suggestion.

I'm pretty sure you add the following to your functions file:

add_action( 'parse_request', 'category_search_logic', 11 );
function category_search_logic( $query ) {

    if ( ! isset( $query->query_vars[ 'cat' ] ) )
        return $query;

    // split cat query on a space to get IDs separated by '+' in URL
    $cats = explode( ' ', $query->query_vars[ 'cat' ] );

    if ( count( $cats ) > 1 ) {
        unset( $query->query_vars[ 'cat' ] );
        $query->query_vars[ 'category__and' ] = $cats;
    }

    return $query;
}

But, how do I incorporate this into the search form? I'm sure it's something simple, but I'm lost...anyone have any ideas?

Thanks, Josh

0

1 Answer 1

2

I found the solution...

My functions page now looks like:

<?php
    class dropdown extends Walker_CategoryDropdown {
        function start_el(&$output, $category, $depth, $args) {
            $pad = str_repeat('&nbsp;', $depth * 3);
            $cat_name = apply_filters( 'list_cats', $category->name, $category );
            $output .= "\t<option class=\"level-$depth\" value=\"".$category->slug."\""; 
            $output .= '>';
            $output .= $pad.$cat_name;
            if ( $args['show_count'] )
                $output .= '&nbsp;&nbsp;('. $category->count .')';
            if ( $args['show_last_update'] ) {
                $format = 'Y-m-d';
            $output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
            }
            $output .= "</option>\n";
        }
    }
?>

My form now looks like:

<form method="get" id="searchform" action="<?php echo home_url(); ?>">
    <input type="text" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Product Search...':this.value;" value="Product Search..." name="s" id="s" class="left" />
    <input type="image" src="<?php bloginfo('template_directory') ?>/images/icosearch.png" id="searchsubmit" value=""/>
    <?php
        wp_dropdown_categories(
            array(
                'child_of' => 426,
                'class' => 'styled',
                'id' => 'make',
                'name' => 'make',
                'show_option_all' => 'Make...',
                'taxonomy' => 'product_cat',
                'walker' => new dropdown
            ))
    ?>
    <?php
        wp_dropdown_categories(
            array(
                'child_of' => 427,
                'class' => 'styled',
                'id' => 'model',
                'name' => 'model',
                'show_option_all' => 'Model...',
                'taxonomy' => 'product_cat',
                'walker' => new dropdown
            ))
    ?>
</form>

My search page now looks like:

<?php
    $tmp[]=$_GET["model"];
    $tmp[]=$_GET["make"];
    $product_cat=array();                                          
    foreach($tmp as $v) {
        if($v!="0")
        $product_cat[]=$v;
    }
    if(count($product_cat)>0) {
        $product_cat="&product_cat=".implode(",",$product_cat);    
    }
    else {
        $product_cat="";
    }                                                           
    $query=query_posts("s=$text".$product_cat);
    $count =count($query);
    if ($count == 0) { 
        echo 'No Results'; 
    }
    elseif ($count == 1) { 
        echo ''.$count.' Result'; 
    } 
    else { 
        echo ''.$count.' Results'; 
    } 
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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