Searching by category not working

I am working with gridmarket theme. I really need to be able to have a search form that searches within two categories. I have found the code to make it work. No prob with that.

Question: I have noticed that when I go to categories using other plugins, sometimes they dont recognize my store product categories as categories. I have created the search form with the hidden fields and everything, but it doesn't seem to be finding my categories within the store? I am guessing that may be the issue...if you know anything please help

  • Ash

    Hello @John

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    I have noticed that when I go to categories using other plugins, sometimes they dont recognize my store product categories as categories.

    It depends on that 'other' plugin, how they are grabbing custom taxonomies. Product categories are not wordpress default categories, they are custom taxonomies. So, you need to write/edit code so that it can take those custom taxonomies.

    If you please show me the codes that they are using to get categories, I may test something for you.

    Please let me know.


  • John

    Hey Ash,

    Okay. Something simple

    <form method="get" action="/">
    <input type="text" name="s" id="s" />
    <input type="hidden" name="cat" id="cat" value="55" />
    <input type="submit" value="Search">

    Most of the plugins that I have tried with search don't see the product categories. You explained it very well. I had a feeling something was strange...

    Thanks again Ask your help is appreciated!


  • Ash

    Okay I have a solution for you.

    Add the following code in your functions.php:

    function get_terms_dropdown($taxonomies, $args){
    	$myterms = get_terms($taxonomies, $args);
    	$optionname = &quot;optionname&quot;;
    	$emptyvalue = &quot;&quot;;
    	$output =&quot;<select name='&quot;.$optionname.&quot;'><option selected='&quot;.$selected.&quot;' value='&quot;.$emptyvalue.&quot;'>Select a Category</option>'&quot;;
    	foreach($myterms as $term){
    		$term_taxonomy=$term->product_category; //CHANGE ME
    		$term_name =$term->name;
    		$link = $term_slug;
    		$output .=&quot;<option name='&quot;.$link.&quot;' value='&quot;.$link.&quot;'>&quot;.$term_name.&quot;</option>&quot;;
    	$output .=&quot;</select>&quot;;
    return $output;

    Add the following code where you want the search form:

    <form role=&quot;search&quot; method=&quot;get&quot; id=&quot;searchform&quot; action=&quot;<?php bloginfo('home'); ?>&quot;>
        <input type=&quot;text&quot; value=&quot;&quot; name=&quot;s&quot; id=&quot;s&quot; />
    $taxonomies = array('product_category');
    $args = array('order'=>'ASC','hide_empty'=>true);
    echo get_terms_dropdown($taxonomies, $args);
    <input type=&quot;submit&quot; id=&quot;searchsubmit&quot; value=&quot;Search&quot; />

    And add this lines in your search.php template:

    global $query_string;
    $termslug = $_GET['optionname'];
    if (!empty($termslug)) query_posts(array('product_category' => $termslug) );

    I guess you will be good at styling :slight_smile:

    Please check if it works.

    Please let me know if you find this is helpful, or if you have any further questions.


Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.