Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-16T13:14:30.543Z
Answers.LastActivityDate: 2017-10-16T13:14:30.543Z
Answers.UserDisplayName: Marion Newlevant
Body: <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>
  
Tags: <entries><categories><search><filter>
Title: Filtering Search Results by Category
ThreadText: # 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](https://craftcookbook.net/recipes/universal-category-filters) 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>
  
  ```
  
  **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):
  
  ```
  <a href="{{craft.request.url}}/?category={{ category.slug }}">
    {{ category.title}}
  </a>
  
  ```
  
  You are generating the url by from [craft.request.url](https://craftcms.com/docs/templating/craft.request#getUrl) 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](https://craftcms.com/docs/templating/functions#url) function, passing it the query string parameters.
  
  
  ---
  
  