<p>You can build you categories dropdown using the <a href="http://buildwithcraft.com/docs/templating/craft.categories" rel="nofollow"><code>craft.categories</code> tag</a>.  Your Form submission field will end up looking something like this in HTML:</p>

<pre><code>&lt;form action="search/results" method="get"&gt;
    &lt;select name="querySlug"&gt;
      &lt;option value="*"&gt;All products&lt;/option&gt;
      &lt;option value="fruit"&gt;Fruit&lt;/option&gt;
      &lt;option value="vegetables"&gt;Vegetables&lt;/option&gt;
      &lt;option value="dairy"&gt;Dairy&lt;/option&gt;
    &lt;/select&gt;
    &lt;input type="submit" value="Search"&gt;
&lt;/form&gt;
</code></pre>

<p>Your results page will behave similar to the <a href="http://buildwithcraft.com/docs/templating/search-form" rel="nofollow">example results page</a> in the Craft docs, except, instead of passing your query to a <code>craft.entries.search()</code> tag, you'll want to pass it to the <code>craft.categories.slug()</code> tag and then hand off the CategoryModel you get back to the <code>craft.entries</code> tag using <code>relatedTo()</code>:</p>

<pre><code>{% set querySlug = craft.request.getParam('querySlug') %}
{% set category = craft.categories.group('productCategories').slug(querySlug) %}
{% set entries = craft.entries.section('products').relatedTo(category) %}
</code></pre>

<p>All categories are returned by default, so you just have to handle that in a conditional if somebody selects that option.  Here is a simple way you could do that:</p>

<pre><code>{% set querySlug = craft.request.getParam('querySlug') %}
{% set category = craft.categories.group('productCategories').slug(querySlug) %}

{% if querySlug == "*" %}   
    {% set products = craft.entries.section('products') %}
{% else %}
    {% set products = craft.entries.section('products').relatedTo(category) %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-08-16T17:05:47.203Z
Hey Ben.  Just what I needed.  Thank you very much.
user1663102
CC BY-SA 3.0
2015-08-15T16:23:53.047Z
2015-08-15T16:23:53.047Z
Ben Parizek
<p>I am relatively new to Craft and I am having a little trouble understanding how to build a simple search which will allow me to have dropdown of my categories which will return the selected category and return results on /products/results page.</p>

<p>I have a category called "Products" and have the following categories "Fruit", "Vegetables", "Dairy", "Herbs", "Eggs", and "Misc".  I would like to have these categories in a dropdown menu which would allow visitor to select the category they would like to view.  I also need it to show all product when they select "All Products", with "All Products" being the default value.</p>

<p>Thank you for your help.</p>

<categories><form>
Simple search using a single category in Craft CMS
# Simple search using a single category in Craft CMS

(Asked by: username_0 on 2015-07-03)

I am relatively new to Craft and I am having a little trouble understanding how to build a simple search which will allow me to have dropdown of my categories which will return the selected category and return results on /products/results page.

I have a category called "Products" and have the following categories "Fruit", "Vegetables", "Dairy", "Herbs", "Eggs", and "Misc". I would like to have these categories in a dropdown menu which would allow visitor to select the category they would like to view. I also need it to show all product when they select "All Products", with "All Products" being the default value.

Thank you for your help.

**Tags:** categories, form

---

## 1 answers

### Answer by username_1 on 2015-08-15. Score: 2

You can build you categories dropdown using the craft.categories tag.  Your Form submission field will end up looking something like this in HTML:

    
      All products
      Fruit
      Vegetables
      Dairy
    
    


Your results page will behave similar to the example results page in the Craft docs, except, instead of passing your query to a craft.entries.search() tag, you'll want to pass it to the craft.categories.slug() tag and then hand off the CategoryModel you get back to the craft.entries tag using relatedTo():
{% set querySlug = craft.request.getParam('querySlug') %}
{% set category = craft.categories.group('productCategories').slug(querySlug) %}
{% set entries = craft.entries.section('products').relatedTo(category) %}

All categories are returned by default, so you just have to handle that in a conditional if somebody selects that option.  Here is a simple way you could do that:
{% set querySlug = craft.request.getParam('querySlug') %}
{% set category = craft.categories.group('productCategories').slug(querySlug) %}

{% if querySlug == "*" %}   
    {% set products = craft.entries.section('products') %}
{% else %}
    {% set products = craft.entries.section('products').relatedTo(category) %}
{% endif %}

> **Comments:**
> **username_0**: Hey Ben.  Just what I needed.  Thank you very much.  

---

