Answers.Body: <p>Let's get the caveats dispensed with. I <em>think</em> the approach detailed in this answer should work, but I've never tried relating categories to categories in this way before.</p>
  
  <p>Speaking of which, I have some misgivings regarding the way you've organised everything. However, let's assume for the time being that you have good reasons for doing it this way.</p>
  
  <p>You're currently attempting to search for entries (note the lowercase "e") which contain a given string, such as the county name. That's a scattershot approach, so it's not surprising that you're getting some unwanted results.</p>
  
  <p>In order to specify the relationships between you property types and the counties, developments, etc., <strong>you'll need to use the category ID as the drop down value, not the category slug</strong>.</p>
  
  <p>Given that you only want to filter by one criterion at a time, the easiest solution is to do something like this:</p>
  
  <pre><code>{% set countyId = craft.request.getParam('countyId')|default('') %}
  {# And so forth, for each filter #}
  
  {% if countyId %}
      {% set relatedCriteria = {
          targetElement: countyId,
          field: 'countyFieldHandle'
      } %}
  {% endif %}
  
  {# And so forth, for each filter, in order of preference #}
  
  {% set propertyTypes = relatedCriteria|default(false)
  ? craft.categories.group('developments').relatedTo(relatedCriteria)
  : craft.categories.group('developments') %}
  
  {% for propertyType in propertyTypes %}
      {# Do your thing #}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-18T18:20:52.517Z
Answers.Text: Thanks for looking at this Stephen. I will give this a try.
Answers.UserDisplayName: mention79
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-18T21:18:21.377Z
Answers.Text: Just use `category.id` instead of `category.slug`
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-18T21:20:02.217Z
Answers.Text: I've tried that, but I'm not understanding how to change the current query with what you've posted above. As I get relatedCriteria variable doesn't exist.
Answers.UserDisplayName: mention79
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-18T21:36:51.270Z
Answers.Text: I've updated the example to handle situations where no filters are set.
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-18T21:49:32.383Z
Answers.Text: Thank you Stephen it appears to be working now, so that's amazing. It's my first Craft site so queries are a bit beyond me, and I still don't understand why what you posted works. But happy it does as the site is due to go live next week and doing final amends and bug fixes.
Answers.UserDisplayName: mention79
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-18T21:55:24.547Z
Answers.Text: That's quite the baptism of fire. Good to hear my answer worked for you.
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-18T21:59:58.910Z
Answers.Text: Sorry, it worked for the counties, but not the other three dropdowns now I've added the code for those. Any ideas?
Answers.UserDisplayName: mention79
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-18T22:01:27.713Z
Answers.Text: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/58982/discussion-between-stephen-lewis-and-mention79).
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-18T17:32:31.230Z
Answers.LastActivityDate: 2017-05-18T21:54:23.360Z
Answers.LastEditDate: 2017-05-18T21:54:23.360Z
Answers.UserDisplayName: Stephen Lewis
Body: <p>I have four select drop downs which let the user filter the results by county, development, price and development status.</p>
  
  <p>However, I'm getting results showing in the wrong/multiple categories for some items once selected. For example there is a development called Lockeswood, but when Lockeswood is selected from development dropdown. The results show Lockeswood and and another development called Highfield.</p>
  
  <p>Another example: The county dropdown lets the user filter the results by county. i.e. Berkshire, Buckinghamshire, Hampshire etc. So when the user selects Berkshire, I only want results from the Berkshire category. However, when selecting Berkshire I'm getting results for Berkshire and Buckinghamshire?</p>
  
  <p>So the select drop downs all show the correct options, but the results are incorrect in some cases.</p>
  
  <p>Can someone help fix the query below to only output the correct results?</p>
  
  <p>Have attached a screenshot highlighting the issue in red.<a href="https://i.sstatic.net/M4b5c.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/M4b5c.jpg" alt="enter image description here"></a></p>
  
  <pre><code>&lt;section class="main secondary"&gt;
    &lt;div class="container"&gt;
      &lt;h1 class=""&gt;{{ entry.title }}&lt;/h1&gt;
      &lt;p class="intro"&gt;{{ entry.introduction }}&lt;/p&gt;
      &lt;div class="row"&gt;
        &lt;div class="col-md-12"&gt;
        &lt;div class="filter-heading clearfix"&gt;
          &lt;div class="filter-block"&gt;
            &lt;span&gt;Refine your search&lt;/span&gt;
          &lt;/div&gt;
          &lt;div class="ld_triangle t-right"&gt;&lt;/div&gt;
        &lt;/div&gt;
        &lt;form class="form-inline" action="{{ url('developments') }}" method="get"&gt;
          &lt;div class="form-group"&gt;
            &lt;label for="county1"&gt;Location&lt;/label&gt;
            &lt;div class="selectwrap"&gt;
              &lt;select id="county1" class="form-control" name="county1" onchange="this.form.submit()"&gt;
            &lt;option value=""&gt;All&lt;/option&gt;
              {% for category in craft.categories.group('counties').order('title asc') %}
              &lt;option value="{{ category.slug }}"&gt;{{ category.title }}&lt;/option&gt;
              {% endfor %}
          &lt;/select&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
        &lt;label for="development1"&gt;Development&lt;/label&gt;
        &lt;div class="selectwrap"&gt;
          &lt;select id="development1" class="form-control" name="development1" onchange="this.form.submit()"&gt;
            &lt;option value=""&gt;All&lt;/option&gt;
            {% for category in craft.categories.group('developments').order('title asc') %}
            &lt;option value="{{ category.slug }}"&gt;{{ category.title }}&lt;/option&gt;
            {% endfor %}
          &lt;/select&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
        &lt;label for="price1"&gt;Price&lt;/label&gt;
          &lt;div class="selectwrap"&gt;
              &lt;select id="price1" class="form-control" name="price1" onchange="this.form.submit()"&gt;
              &lt;option value=""&gt;All&lt;/option&gt;
              {% for category in craft.categories.group('priceRange').order('title asc') %}
              &lt;option value="{{ category.slug }}"&gt;{{ category.title }}&lt;/option&gt;
              {% endfor %}
              &lt;/select&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;div class="form-group"&gt;
      &lt;label for="status1"&gt;Development Status&lt;/label&gt;
      &lt;div class="selectwrap"&gt;
      &lt;select id="status1" class="form-control" name="status1" onchange="this.form.submit()"&gt;
          &lt;option value=""&gt;All&lt;/option&gt;
          {% for category in craft.categories.group('status').order('title asc') %}
          &lt;option value="{{ category.slug }}"&gt;{{ category.title }}&lt;/option&gt;
          {% endfor %}
          &lt;/select&gt;
          &lt;/div&gt;
          &lt;/div&gt;
          &lt;/form&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/section&gt;
  
  &lt;section class="developments tab-content"&gt;
    &lt;div class="container"&gt;
  
      {# Start For loop #}
      {% set query = "" %}
      {% if craft.request.getParam('county1') and
        craft.request.getParam('county1') is not empty %}
      {% set query = craft.request.getParam('county1') %}
      {% elseif craft.request.getParam('development1') and
        craft.request.getParam('development1') is not empty %}
      {% set query = craft.request.getParam('development1') %}
      {% elseif craft.request.getParam('price1') and
        craft.request.getParam('price1') is not empty %}
      {% set query = craft.request.getParam('price1') %}
      {% elseif craft.request.getParam('status1') and
        craft.request.getParam('status1') is not empty %}
      {% set query = craft.request.getParam('status1') %}
      {% endif %}
  
      {% set queryFilter = craft.categories.group('developments').search(query).order('title asc') %}
  
      {% if queryFilter|length %}
      {% for category in queryFilter %}
  &lt;div class="development-block"&gt;
    &lt;a class="grid one" href="{{ category.url }}" role="button"&gt;
      {% set image = category.insetImage.first() %}
      {% for image in category.insetImage %}
      &lt;div class="grid__item" style="background-image:url({{ image.url('devListMainLarge') }})"&gt;&lt;/div&gt;
      {% endfor %}
      {% set imagesecondary = category.insetImageSecondary.first() %}
      {% for imagesecondary in category.insetImageSecondary %}
      &lt;div class="grid__item" style="background-image:url({{ imagesecondary.url('devListSecondaryLarge') }})"&gt;&lt;/div&gt;
      {% endfor %}
    &lt;/a&gt;
    &lt;div class="grid two"&gt;
      &lt;div&gt;
        &lt;h2&gt;&lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;&lt;/h2&gt;
        &lt;h4 class=""&gt;{{ category.town }}, {{ category.county }}&lt;/h4&gt;
        &lt;p class="standfirst"&gt;{{ category.developmentIntroduction}}&lt;/p&gt;
        &lt;p class="indicative"&gt;{{ category.fapCaption}}&lt;/p&gt;
      &lt;/div&gt;
      &lt;div&gt;
        &lt;a class="btn development hidden-xs hidden-sm" href="{{ category.url }}" role="button"&gt;View development&lt;i class="icon-caret-right btn-after"&gt;&lt;/i&gt;&lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="divider"&gt;&lt;/div&gt;
    &lt;aside class="grid three"&gt;
      &lt;div&gt;
        &lt;div class="price"&gt;{{ category.price }}&lt;/div&gt;
        &lt;div class="cta-info"&gt;{{ category.callToAction }}&lt;/div&gt;
      &lt;/div&gt;
      &lt;div&gt;
        &lt;h5 class="agent"&gt;Selling agent:&lt;/h5&gt;
        {% for option in category.agentName %}
        &lt;div&gt;{{ option }}&lt;/div&gt;
        {% else %}
        &lt;div&gt;{{ category.agentNotListed }}&lt;/div&gt;
        {% endfor %}
        &lt;a class="register" data-toggle="modal" href="{{ category.url }}?message=register"&gt;REGISTER YOUR INTEREST&lt;/a&gt;
        &lt;a class="btn development hidden-md hidden-lg" href="{{ category.url }}" role="button"&gt;View development&lt;i class="icon-caret-right btn-after"&gt;&lt;/i&gt;&lt;/a&gt;
      &lt;/div&gt;
    &lt;/aside&gt;
  &lt;/div&gt;
      {% endfor %} {# End For loop #}
      {% else %}
      &lt;p class="intro"&gt;
        There are currently no developments in {{ query|capitalize }}. &lt;a data-toggle="modal" href="#priorityModal"&gt;&lt;strong&gt;Register you interest&lt;/strong&gt;&lt;/a&gt; to receive priority information when a development becomes available in the area.&lt;/p&gt;
      {% endif %}
  </code></pre>
  
  <p>
  </p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-18T16:36:44.053Z
Comments.Text: The problem is how you're constructing the "query". How are you storing these developments? It looks like you're using categories, rather than entries.
Comments.UserDisplayName: Stephen Lewis
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-18T16:37:36.423Z
Comments.Text: Yes they are all created from categories
Comments.UserDisplayName: mention79
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-18T16:40:18.543Z
Comments.Text: And you only want to be able to filter by one of the drop downs at a time? So you can view all properties in a county OR all properties in a development, and so forth.
Comments.UserDisplayName: Stephen Lewis
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-18T16:40:59.627Z
Comments.Text: Yes that's correct
Comments.UserDisplayName: mention79
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-18T16:44:42.460Z
Comments.Text: And how have you implemented the development, county, etc.? As select fields attached to the development categories group?
Comments.UserDisplayName: Stephen Lewis
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-18T16:46:00.213Z
Comments.Text: Yep, you should be able to see that in the code above
Comments.UserDisplayName: mention79
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-18T16:56:47.127Z
Comments.Text: Actually, looking at the code again, you're using categories for those things. Are you relating a property type (e.g. Englemere) to a development, a county, and so forth using custom [Categories fields](https://craftcms.com/docs/categories-fields) attached to the "property type" category group?
Comments.UserDisplayName: Stephen Lewis
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-18T17:03:31.300Z
Comments.Text: Yes the developments have categories fields that select the county etc
Comments.UserDisplayName: mention79
Tags: <queries>
Title: Query is outputting additional results
ThreadText: # Query is outputting additional results
  
  (Asked by: username_0 on 2017-05-18)
  
  I have four select drop downs which let the user filter the results by county, development, price and development status.
  
  However, I'm getting results showing in the wrong/multiple categories for some items once selected. For example there is a development called Lockeswood, but when Lockeswood is selected from development dropdown. The results show Lockeswood and and another development called Highfield.
  
  Another example: The county dropdown lets the user filter the results by county. i.e. Berkshire, Buckinghamshire, Hampshire etc. So when the user selects Berkshire, I only want results from the Berkshire category. However, when selecting Berkshire I'm getting results for Berkshire and Buckinghamshire?
  
  So the select drop downs all show the correct options, but the results are incorrect in some cases.
  
  Can someone help fix the query below to only output the correct results?
  
  Have attached a screenshot highlighting the issue in red.[![enter image description here](https://i.sstatic.net/M4b5c.jpg)](https://i.sstatic.net/M4b5c.jpg)
  
  ```
  <section class="main secondary">
    <div class="container">
      <h1 class="">{{ entry.title }}</h1>
      <p class="intro">{{ entry.introduction }}</p>
      <div class="row">
        <div class="col-md-12">
        <div class="filter-heading clearfix">
          <div class="filter-block">
            <span>Refine your search</span>
          </div>
          <div class="ld_triangle t-right"></div>
        </div>
        <form class="form-inline" action="{{ url('developments') }}" method="get">
          <div class="form-group">
            <label for="county1">Location</label>
            <div class="selectwrap">
              <select id="county1" class="form-control" name="county1" onchange="this.form.submit()">
            <option value="">All</option>
              {% for category in craft.categories.group('counties').order('title asc') %}
              <option value="{{ category.slug }}">{{ category.title }}</option>
              {% endfor %}
          </select>
        </div>
      </div>
      <div class="form-group">
        <label for="development1">Development</label>
        <div class="selectwrap">
          <select id="development1" class="form-control" name="development1" onchange="this.form.submit()">
            <option value="">All</option>
            {% for category in craft.categories.group('developments').order('title asc') %}
            <option value="{{ category.slug }}">{{ category.title }}</option>
            {% endfor %}
          </select>
        </div>
      </div>
      <div class="form-group">
        <label for="price1">Price</label>
          <div class="selectwrap">
              <select id="price1" class="form-control" name="price1" onchange="this.form.submit()">
              <option value="">All</option>
              {% for category in craft.categories.group('priceRange').order('title asc') %}
              <option value="{{ category.slug }}">{{ category.title }}</option>
              {% endfor %}
              </select>
          </div>
        </div>
      <div class="form-group">
      <label for="status1">Development Status</label>
      <div class="selectwrap">
      <select id="status1" class="form-control" name="status1" onchange="this.form.submit()">
          <option value="">All</option>
          {% for category in craft.categories.group('status').order('title asc') %}
          <option value="{{ category.slug }}">{{ category.title }}</option>
          {% endfor %}
          </select>
          </div>
          </div>
          </form>
        </div>
      </div>
    </div>
  </section>
  
  <section class="developments tab-content">
    <div class="container">
  
      {# Start For loop #}
      {% set query = "" %}
      {% if craft.request.getParam('county1') and
        craft.request.getParam('county1') is not empty %}
      {% set query = craft.request.getParam('county1') %}
      {% elseif craft.request.getParam('development1') and
        craft.request.getParam('development1') is not empty %}
      {% set query = craft.request.getParam('development1') %}
      {% elseif craft.request.getParam('price1') and
        craft.request.getParam('price1') is not empty %}
      {% set query = craft.request.getParam('price1') %}
      {% elseif craft.request.getParam('status1') and
        craft.request.getParam('status1') is not empty %}
      {% set query = craft.request.getParam('status1') %}
      {% endif %}
  
      {% set queryFilter = craft.categories.group('developments').search(query).order('title asc') %}
  
      {% if queryFilter|length %}
      {% for category in queryFilter %}
  <div class="development-block">
    <a class="grid one" href="{{ category.url }}" role="button">
      {% set image = category.insetImage.first() %}
      {% for image in category.insetImage %}
      <div class="grid__item" style="background-image:url({{ image.url('devListMainLarge') }})"></div>
      {% endfor %}
      {% set imagesecondary = category.insetImageSecondary.first() %}
      {% for imagesecondary in category.insetImageSecondary %}
      <div class="grid__item" style="background-image:url({{ imagesecondary.url('devListSecondaryLarge') }})"></div>
      {% endfor %}
    </a>
    <div class="grid two">
      <div>
        <h2><a href="{{ category.url }}">{{ category.title }}</a></h2>
        <h4 class="">{{ category.town }}, {{ category.county }}</h4>
        <p class="standfirst">{{ category.developmentIntroduction}}</p>
        <p class="indicative">{{ category.fapCaption}}</p>
      </div>
      <div>
        <a class="btn development hidden-xs hidden-sm" href="{{ category.url }}" role="button">View development<i class="icon-caret-right btn-after"></i></a>
      </div>
    </div>
    <div class="divider"></div>
    <aside class="grid three">
      <div>
        <div class="price">{{ category.price }}</div>
        <div class="cta-info">{{ category.callToAction }}</div>
      </div>
      <div>
        <h5 class="agent">Selling agent:</h5>
        {% for option in category.agentName %}
        <div>{{ option }}</div>
        {% else %}
        <div>{{ category.agentNotListed }}</div>
        {% endfor %}
        <a class="register" data-toggle="modal" href="{{ category.url }}?message=register">REGISTER YOUR INTEREST</a>
        <a class="btn development hidden-md hidden-lg" href="{{ category.url }}" role="button">View development<i class="icon-caret-right btn-after"></i></a>
      </div>
    </aside>
  </div>
      {% endfor %} {# End For loop #}
      {% else %}
      <p class="intro">
        There are currently no developments in {{ query|capitalize }}. <a data-toggle="modal" href="#priorityModal"><strong>Register you interest</strong></a> to receive priority information when a development becomes available in the area.</p>
      {% endif %}
  
  ```
  
  **Tags:** queries
  
  > **Comments:**
  > **username_1**: The problem is how you're constructing the "query". How are you storing these developments? It looks like you're using categories, rather than entries.  
  > **username_0**: Yes they are all created from categories  
  > **username_1**: And you only want to be able to filter by one of the drop downs at a time? So you can view all properties in a county OR all properties in a development, and so forth.  
  > **username_0**: Yes that's correct  
  > **username_1**: And how have you implemented the development, county, etc.? As select fields attached to the development categories group?  
  > **username_0**: Yep, you should be able to see that in the code above  
  > **username_1**: Actually, looking at the code again, you're using categories for those things. Are you relating a property type (e.g. Englemere) to a development, a county, and so forth using custom [Categories fields](https://craftcms.com/docs/categories-fields) attached to the "property type" category group?  
  > **username_0**: Yes the developments have categories fields that select the county etc  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-05-18. Score: 2 | Accepted answer
  
  Let's get the caveats dispensed with. I *think* the approach detailed in this answer should work, but I've never tried relating categories to categories in this way before.
  
  Speaking of which, I have some misgivings regarding the way you've organised everything. However, let's assume for the time being that you have good reasons for doing it this way.
  
  You're currently attempting to search for entries (note the lowercase "e") which contain a given string, such as the county name. That's a scattershot approach, so it's not surprising that you're getting some unwanted results.
  
  In order to specify the relationships between you property types and the counties, developments, etc., **you'll need to use the category ID as the drop down value, not the category slug**.
  
  Given that you only want to filter by one criterion at a time, the easiest solution is to do something like this:
  
  ```
  {% set countyId = craft.request.getParam('countyId')|default('') %}
  {# And so forth, for each filter #}
  
  {% if countyId %}
      {% set relatedCriteria = {
          targetElement: countyId,
          field: 'countyFieldHandle'
      } %}
  {% endif %}
  
  {# And so forth, for each filter, in order of preference #}
  
  {% set propertyTypes = relatedCriteria|default(false)
  ? craft.categories.group('developments').relatedTo(relatedCriteria)
  : craft.categories.group('developments') %}
  
  {% for propertyType in propertyTypes %}
      {# Do your thing #}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for looking at this username_1. I will give this a try.  
  > **username_1**: Just use `category.id` instead of `category.slug`  
  > **username_0**: I've tried that, but I'm not understanding how to change the current query with what you've posted above. As I get relatedCriteria variable doesn't exist.  
  > **username_1**: I've updated the example to handle situations where no filters are set.  
  > **username_0**: Thank you username_1 it appears to be working now, so that's amazing. It's my first Craft site so queries are a bit beyond me, and I still don't understand why what you posted works. But happy it does as the site is due to go live next week and doing final amends and bug fixes.  
  > **username_1**: That's quite the baptism of fire. Good to hear my answer worked for you.  
  > **username_0**: Sorry, it worked for the counties, but not the other three dropdowns now I've added the code for those. Any ideas?  
  > **username_1**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/58982/discussion-between-username_1-username_1-and-username_0).  
  
  ---
  
  