Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-18T17:50:56.587Z
Answers.LastActivityDate: 2015-11-18T17:50:56.587Z
Answers.UserDisplayName: Brandon Kelly
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-14T16:15:38.503Z
Answers.Text: 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.
Answers.UserDisplayName: jthomas
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-15T17:24:08.200Z
Answers.Text: Hey No worries, pleasure :)
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-13T11:50:42.237Z
Answers.LastActivityDate: 2015-11-13T11:50:42.237Z
Answers.UserDisplayName: Oli
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-12T20:13:18.983Z
Answers.Text: 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.
Answers.UserDisplayName: jthomas
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-12T14:27:15.030Z
Answers.LastActivityDate: 2015-11-12T14:27:15.030Z
Answers.UserDisplayName: Oli
Body: <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>
  
Tags: <categories><relations>
Title: Perform inclusive relatedTo query with multiple categories in Craft 2.5+
ThreadText: # 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](https://craftcms.stackexchange.com/questions/2747/perform-inclusive-relatedto-query-with-multiple-categories/2795#comment17006_2795), @username_1 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 username_1'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.  
  
  ---
  
  