<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>

CC BY-SA 3.0
2017-05-18T18:20:52.517Z
Thanks for looking at this Stephen. I will give this a try.
mention79
CC BY-SA 3.0
2017-05-18T21:18:21.377Z
Just use `category.id` instead of `category.slug`
Stephen Lewis
CC BY-SA 3.0
2017-05-18T21:20:02.217Z
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.
mention79
CC BY-SA 3.0
2017-05-18T21:36:51.270Z
I've updated the example to handle situations where no filters are set.
Stephen Lewis
CC BY-SA 3.0
2017-05-18T21:49:32.383Z
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.
mention79
CC BY-SA 3.0
2017-05-18T21:55:24.547Z
That's quite the baptism of fire. Good to hear my answer worked for you.
Stephen Lewis
CC BY-SA 3.0
2017-05-18T21:59:58.910Z
Sorry, it worked for the counties, but not the other three dropdowns now I've added the code for those. Any ideas?
mention79
CC BY-SA 3.0
2017-05-18T22:01:27.713Z
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/58982/discussion-between-stephen-lewis-and-mention79).
Stephen Lewis
CC BY-SA 3.0
2017-05-18T17:32:31.230Z
2017-05-18T21:54:23.360Z
2017-05-18T21:54:23.360Z
Stephen Lewis
<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>

CC BY-SA 3.0
2017-05-18T16:36:44.053Z
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.
Stephen Lewis
CC BY-SA 3.0
2017-05-18T16:37:36.423Z
Yes they are all created from categories
mention79
CC BY-SA 3.0
2017-05-18T16:40:18.543Z
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.
Stephen Lewis
CC BY-SA 3.0
2017-05-18T16:40:59.627Z
Yes that's correct
mention79
CC BY-SA 3.0
2017-05-18T16:44:42.460Z
And how have you implemented the development, county, etc.? As select fields attached to the development categories group?
Stephen Lewis
CC BY-SA 3.0
2017-05-18T16:46:00.213Z
Yep, you should be able to see that in the code above
mention79
CC BY-SA 3.0
2017-05-18T16:56:47.127Z
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?
Stephen Lewis
CC BY-SA 3.0
2017-05-18T17:03:31.300Z
Yes the developments have categories fields that select the county etc
mention79
<queries>
Query is outputting additional results
# 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.

  
    {{ entry.title }}
    {{ entry.introduction }}
    
      
      
        
          Refine your search
        
        
      
      
        
          Location
          
            
          All
            {% for category in craft.categories.group('counties').order('title asc') %}
            {{ category.title }}
            {% endfor %}
        
      
    
    
      Development
      
        
          All
          {% for category in craft.categories.group('developments').order('title asc') %}
          {{ category.title }}
          {% endfor %}
        
      
    
    
      Price
        
            
            All
            {% for category in craft.categories.group('priceRange').order('title asc') %}
            {{ category.title }}
            {% endfor %}
            
        
      
    
    Development Status
    
    
        All
        {% for category in craft.categories.group('status').order('title asc') %}
        {{ category.title }}
        {% endfor %}
        
        
        
        
      
    
  



  

    {# 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 %}

  
    {% set image = category.insetImage.first() %}
    {% for image in category.insetImage %}
    
    {% endfor %}
    {% set imagesecondary = category.insetImageSecondary.first() %}
    {% for imagesecondary in category.insetImageSecondary %}
    
    {% endfor %}
  
  
    
      {{ category.title }}
      {{ category.town }}, {{ category.county }}
      {{ category.developmentIntroduction}}
      {{ category.fapCaption}}
    
    
      View development
    
  
  
  
    
      {{ category.price }}
      {{ category.callToAction }}
    
    
      Selling agent:
      {% for option in category.agentName %}
      {{ option }}
      {% else %}
      {{ category.agentNotListed }}
      {% endfor %}
      REGISTER YOUR INTEREST
      View development
    
  

    {% endfor %} {# End For loop #}
    {% else %}
    
      There are currently no developments in {{ query|capitalize }}. Register you interest to receive priority information when a development becomes available in the area.
    {% 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 Stephen. 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 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.  
> **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-stephen-lewis-and-username_0).  

---

