<p>Some of the unexpected behavior here ended up being due to a bug in the new 2.5 relatedTo syntax. It was fixed in <a href="http://buildwithcraft.com/beta#build2724" rel="nofollow">Craft 2.5.2724 Beta</a>.</p>

CC BY-SA 3.0
2015-11-18T17:50:56.587Z
2015-11-18T17:50:56.587Z
Brandon Kelly
<p>Just done some testing here and I think it's a bug (I might be wrong).</p>

<p>I don't know how to explain this very well and I'm not sure if it's posted in the right place but here goes...</p>

<p>In the ElementRelationParamParser Class, parseRelationParam function (line 69), this test</p>

<pre><code>if ($relatedTo[0] == 'and' || $relatedTo[0] == 'or')
{
    $glue = array_shift($relatedTo);

}
else
{
    $glue = 'or';
}
</code></pre>

<p>The 'and' or 'or' is actually here in your case</p>

<pre><code>$relatedTo[0]['targetElement'][0]
</code></pre>

<p>So basically, <strong>craft is always defaulting to 'or' at the moment</strong> from what I can see when using the new syntax (Old syntax works for me too btw).</p>

CC BY-SA 3.0
2015-11-14T16:15:38.503Z
Thanks for digging in to this, Oliver. I'll ping the craft guys as I'm sure they'll want to get this fixed in the beta.
jthomas
CC BY-SA 3.0
2015-11-15T17:24:08.200Z
Hey No worries, pleasure :)
Oli
CC BY-SA 3.0
2015-11-13T11:50:42.237Z
2015-11-13T11:50:42.237Z
Oli
<p>I can't test right now but just looking at this, could it be because your array has indexes whereas Brandon's demo is only the list of ids..?</p>

CC BY-SA 3.0
2015-11-12T20:13:18.983Z
That's just the output of the dump() method for twig. It's just showing the values at normal array indexes of 0, 1, and 2.
jthomas
CC BY-SA 3.0
2015-11-12T14:27:15.030Z
2015-11-12T14:27:15.030Z
Oli
<p>I'm trying to to display entries related to "all" the given categories instead of "any" using the updated relatedTo method provided in Craft 2.5.</p>

<p>In a <a href="https://craftcms.stackexchange.com/questions/2747/perform-inclusive-relatedto-query-with-multiple-categories/2795#comment17006_2795">related question</a>, @BrandonKelly explains how to do this in the "Updated" portion of his answer. However, I still seem to be getting "any" behavior instead of entries matching "all" categories.</p>

<p>Here is an example URL if only one category in the Social Services group is selected</p>

<pre><code>http://website.com/technologies/someSocialService
</code></pre>

<p>Here is an example URL if additional technology categories are also added:</p>

<pre><code>http://website.com/technologies/someSocialService?type[]=88&amp;type[]=117
</code></pre>

<p>So, ideally, I want to first filter the entries by the social service category, determined by segment 2 in the URL. </p>

<p>Then, I want to filter even further if there are types of technology in the URL.</p>

<p>Here is my code:</p>

<pre><code>{% set slug = craft.request.getSegment(2) %}
{% set socialServiceCategory = craft.categories.group('socialServices').slug(slug).first() %}
{% set socialServiceCategoryId = socialServiceCategory.id %}

{% set technologyCategoryIds = [] %}
{% if craft.request.getParam('type') %}
{% set technologyCategoryIds = technologyCategoryIds|merge(craft.request.getParam('type')) %}
{% endif %}

{% set categoryIds = [socialServiceCategoryId]|merge(technologyCategoryIds) %}

{% set entries = craft.entries({
    section: 'solutions',
    relatedTo: { 
        targetElement: ['and']|merge(categoryIds)
    }
}).order('listingType desc', 'title asc') %}
</code></pre>

<p>When the page loads with only the segment slug, and no query string, I get entries filtered by that category. However, once I add one or more technology IDs to the categoryIds array, I get all entries matching "any" categories in the categoryIds array.</p>

<p>Given a URL such as the 2nd example I displayed above, here is a dump of my categoryIds output:</p>

<pre><code>array(3) { [0]=&gt; string(2) "15" [1]=&gt; string(2) "88" [2]=&gt; string(3) "117" }
</code></pre>

<p>...which contains <em>exactly</em> the right categories I'd like to inclusively relate my entries to.</p>

<p>For the time being, I was able to get this working the way I need with pre-Craft 2.5 beta syntax for relatedTo using the following code:</p>

<pre><code>{% set entries = entries.relatedTo(socialServiceCategoryId).order('listingType desc', 'title asc') %}

{% if technologyCategoryIds|length %}
{% set entries = entries.relatedTo(
    'and',
    { targetElement: socialServiceCategoryId },
    { targetElement: technologyCategoryIds|join(',') }
).order('listingType desc', 'title asc') %}
{% endif %}
</code></pre>

<categories><relations>
Perform inclusive relatedTo query with multiple categories in Craft 2.5+
# Perform inclusive relatedTo query with multiple categories in Craft 2.5+

(Asked by: username_0 on 2015-11-11)

I'm trying to to display entries related to "all" the given categories instead of "any" using the updated relatedTo method provided in Craft 2.5.
In a related question, @BrandonKelly explains how to do this in the "Updated" portion of his answer. However, I still seem to be getting "any" behavior instead of entries matching "all" categories.
Here is an example URL if only one category in the Social Services group is selected
http://website.com/technologies/someSocialService

Here is an example URL if additional technology categories are also added:
http://website.com/technologies/someSocialService?type[]=88&type[]=117

So, ideally, I want to first filter the entries by the social service category, determined by segment 2 in the URL. 
Then, I want to filter even further if there are types of technology in the URL.
Here is my code:
{% set slug = craft.request.getSegment(2) %}
{% set socialServiceCategory = craft.categories.group('socialServices').slug(slug).first() %}
{% set socialServiceCategoryId = socialServiceCategory.id %}

{% set technologyCategoryIds = [] %}
{% if craft.request.getParam('type') %}
{% set technologyCategoryIds = technologyCategoryIds|merge(craft.request.getParam('type')) %}
{% endif %}

{% set categoryIds = [socialServiceCategoryId]|merge(technologyCategoryIds) %}

{% set entries = craft.entries({
    section: 'solutions',
    relatedTo: { 
        targetElement: ['and']|merge(categoryIds)
    }
}).order('listingType desc', 'title asc') %}

When the page loads with only the segment slug, and no query string, I get entries filtered by that category. However, once I add one or more technology IDs to the categoryIds array, I get all entries matching "any" categories in the categoryIds array.
Given a URL such as the 2nd example I displayed above, here is a dump of my categoryIds output:
array(3) { [0]=> string(2) "15" [1]=> string(2) "88" [2]=> string(3) "117" }

...which contains exactly the right categories I'd like to inclusively relate my entries to.
For the time being, I was able to get this working the way I need with pre-Craft 2.5 beta syntax for relatedTo using the following code:
{% set entries = entries.relatedTo(socialServiceCategoryId).order('listingType desc', 'title asc') %}

{% if technologyCategoryIds|length %}
{% set entries = entries.relatedTo(
    'and',
    { targetElement: socialServiceCategoryId },
    { targetElement: technologyCategoryIds|join(',') }
).order('listingType desc', 'title asc') %}
{% endif %}

**Tags:** categories, relations

---

## 3 answers

### Answer by username_1 on 2015-11-18. Score: 1 | Accepted answer

Some of the unexpected behavior here ended up being due to a bug in the new 2.5 relatedTo syntax. It was fixed in [Craft 2.5.2724 Beta](http://buildwithcraft.com/beta#build2724).


---

### Answer by username_2 on 2015-11-13. Score: 0

Just done some testing here and I think it's a bug (I might be wrong).
I don't know how to explain this very well and I'm not sure if it's posted in the right place but here goes...
In the ElementRelationParamParser Class, parseRelationParam function (line 69), this test
if ($relatedTo[0] == 'and' || $relatedTo[0] == 'or')
{
    $glue = array_shift($relatedTo);

}
else
{
    $glue = 'or';
}

The 'and' or 'or' is actually here in your case
$relatedTo[0]['targetElement'][0]

So basically, craft is always defaulting to 'or' at the moment from what I can see when using the new syntax (Old syntax works for me too btw).

> **Comments:**
> **username_0**: Thanks for digging in to this, Oliver. I'll ping the craft guys as I'm sure they'll want to get this fixed in the beta.  
> **username_2**: Hey No worries, pleasure :)  

---

### Answer by username_2 on 2015-11-12. Score: 0

I can't test right now but just looking at this, could it be because your array has indexes whereas Brandon's demo is only the list of ids..?

> **Comments:**
> **username_0**: That's just the output of the dump() method for twig. It's just showing the values at normal array indexes of 0, 1, and 2.  

---

