<p>In this line here (reformatted for legibilty):</p>

<pre><code>&lt;a href="{{craft.request.url}}/?category={{ category.slug }}"&gt;
  {{ category.title}}
&lt;/a&gt;
</code></pre>

<p>You are generating the url by from <a href="https://craftcms.com/docs/templating/craft.request#getUrl" rel="nofollow noreferrer">craft.request.url</a> 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:</p>

<pre><code>&lt;a href="{{ url(craft.request.path, {q: query, category: category.slug}) }}"&gt;
  {{ category.title}}
&lt;/a&gt;
</code></pre>

<p>This uses the <a href="https://craftcms.com/docs/templating/functions#url" rel="nofollow noreferrer">url</a> function, passing it the query string parameters.</p>

CC BY-SA 3.0
2017-10-16T13:14:30.543Z
2017-10-16T13:14:30.543Z
Marion Newlevant
<p>I'm currently trying to filter my search results by category, I've been following <a href="https://craftcookbook.net/recipes/universal-category-filters" rel="nofollow noreferrer">this tutorial</a> but cannot get it to work. It keeps not adding on the category parameter, but instead just replaces the search parameter like so:</p>

<p><strong>On search:</strong>
"/search/?q=dress"</p>

<p><strong>On Clicking a Filter:</strong>
"/search/?category=wedges"</p>

<p>Below is the code running my search box:</p>

<pre><code>&lt;div class="search-box w-form"&gt;
  &lt;form action="search/" class="form"&gt;
    &lt;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"&gt;
    &lt;input type="submit" data-wait="Please wait..." class="search-field-go w-button" value=""&gt;
  &lt;/form&gt;
&lt;/div&gt;
</code></pre>

<p>and the code running the results page:</p>

<pre><code>{% 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' %}

&lt;!-- SEARCH CONTAINER --&gt;

{% if entries|length %}
    &lt;p&gt;{{ entries|length }} results:&lt;/p&gt;

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

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

<entries><categories><search><filter>
Filtering Search Results by Category
# Filtering Search Results by Category

(Asked by: username_0 on 2017-10-14)

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:

  
    
    
  


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' %}



{% if entries|length %}
    {{ entries|length }} results:

    
        {% for entry in entries %}
            {{ entry.title }}
        {% endfor %}
    
{% else %}
    Your search for “{{ query }}” didn’t return any results.
{% endif %}


   {% for category in craft.categories.group('womens').find() %}
    
            {{ category.title}} 
         
  {% endfor %}

**Tags:** entries, categories, search, filter

---

## 1 answers

### Answer by username_1 on 2017-10-16. Score: 2 | Accepted answer

In this line here (reformatted for legibilty):

  {{ category.title}}


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:

  {{ category.title}}


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


---

