1

I'm currently trying to filter my search results by category, I've been following this tutorial but cannot get it to work. It keeps not adding on the category parameter, but instead just replaces the search parameter like so:

On search: "/search/?q=dress"

On Clicking a Filter: "/search/?category=wedges"

Below is the code running my search box:

<div class="search-box w-form">
  <form action="search/" class="form">
    <input type="text" class="text-field w-input" maxlength="256" type="search" autofocus="true" name="q" data-name="Name" placeholder="e.g. Black Dress" id="name">
    <input type="submit" data-wait="Please wait..." class="search-field-go w-button" value="">
  </form>
</div>

and the code running the results page:

{% set query = craft.request.getParam('q') %}
{% if craft.request.getParam('category') is not null %}
    {% set category = craft.categories.slug(craft.request.getParam('category')) %}
    {% set entries = craft.entries.search(query).order('score').relatedTo(category).find() %}
{% else %}
    {% set entries = craft.entries.search(query).order('score') %}
{% endif %}

{% include 'search/_filters' %}

<!-- SEARCH CONTAINER -->

{% if entries|length %}
    <p>{{ entries|length }} results:</p>

    <ul>
        {% for entry in entries %}
            <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
        {% endfor %}
    </ul>
{% else %}
    <p>Your search for “{{ query }}” didn’t return any results.</p>
{% endif %}

<ul>
   {% for category in craft.categories.group('womens').find() %}
    <li>
            <a href="{{craft.request.url}}/?category={{ category.slug }}">{{ category.title}} </a>
         </li>
  {% endfor %}
</ul>
2

In this line here (reformatted for legibilty):

<a href="{{craft.request.url}}/?category={{ category.slug }}">
  {{ category.title}}
</a>

You are generating the url by from craft.request.url and appending the category. But the original query is not part of the url at this point. I think this will work and be clearer:

<a href="{{ url(craft.request.path, {q: query, category: category.slug}) }}">
  {{ category.title}}
</a>

This uses the url function, passing it the query string parameters.

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.