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.

1 Answer 1


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

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

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 %}
  • Hey Ben. Just what I needed. Thank you very much. Aug 16, 2015 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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