<p>Not 100% sure if this is correct, but this works.</p>

<p>Example urls:</p>

<pre><code>www.mysite.com/cars/corvette/red
www.mysite.com/cars/red/corvette
...
www.mysite.com/cars/red/
www.mysite.com/cars/corvette
</code></pre>

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

<pre><code>{#
 # Check if there are multiple url segments to compare against.
 # This means we need to check against multiple relationships.
 # Ex: All Corvettes that are also Red
 #
 #}
{% if craft.request.getSegment(4)|length %}
    {% set secondaryCategory = craft.categories.slug(craft.request.getSegment(4)).first() %}
    {% set entries = craft.entries.section('mySection').relatedTo(['and', { targetElement: primaryCategory }, { targetElement: secondaryCategory }]) %}
{% else %}
    {#
     # Get all products related to the current category.
     # Ex: All Sports Cars that are related to Corvettes.
     #
     #}
    {% set entries = craft.entries.relatedTo(primaryCategory) %}
{% endif %}

...

{% if entries|length %}
    ...
{% endif %}
</code></pre>

<p>What i'm not sure about is both are using <code>targetElement</code> instead of one being <code>sourceElement</code>.</p>

CC BY-SA 3.0
2015-12-03T19:38:51.373Z
2015-12-03T19:38:51.373Z
Damon
<p>I have two category groups: <code>Cars</code> and <code>Colors</code></p>

<p>My <code>Cars</code> category looks like this:</p>

<pre><code>Cars
    Sports Cars
        Corvette
        Lamborghini
        Porche
</code></pre>

<p>My <code>Colors</code> category looks like this:</p>

<pre><code>Colors
    Red
    Black
    White
</code></pre>

<p>I have two drop-down menus; one to choose the car, the other to choose the color.</p>

<p>I can choose the car, or the color just fine. My url's look like this:</p>

<pre><code>mysite.com/cars/corvette/
- or - 
mysite.com/cars/red/
</code></pre>

<p>It's when I need to relate <em>both</em> categories. Since my two category groups have different levels, I'm not sure how to get the parent group of <code>Colors</code> from <code>red</code>. Which is what I think I need in order for the <code>relatedTo</code> to work properly.</p>

<p>For example, a url might look like this:</p>

<pre><code>mysite.com/cars/corvette/  {# no problem #}  
- or - 
mysite.com/cars/red/       {# no problem #}
</code></pre>

<p>I'm able to get to the categories on my template:</p>

<pre><code>{# 
 # Get the primary category GROUP via url segment. 
 # Example: Cars
 #
 #}
{% set primaryCategoryGroup = craft.categories.slug(craft.request.getSegment(2)).first() %}

{#
 # Get the primary CATEGORY via url segment.
 # Ex: Corvette or Red
 #}
{% set primaryCategory = craft.categories.slug(craft.request.getSegment(3)).first() %}
</code></pre>

<p>Then I check if I need to compare both relationships:</p>

<pre><code>{% if craft.request.getSegment(4)|length %}
      {% set secondaryCategorySlug = craft.categories.slug(craft.request.getSegment(4)) %}
      {% set secondaryCategory = craft.categories.slug(craft.request.getSegment(4)).first() %}

      {{ secondaryCategory.title }}  {# Red or Corvette #}
      {{ primaryCategory }}  {# Red or Corvette #}

      {# Begin the fail #}
      {% set entries = craft.entries.section('mySection').relatedTo('and',
          { sourceElement: secondaryCategory, field:'title' },
          { targetElement: primaryCategory }
      ) %}
{% else %}
    {# Only get single relationship. #}
    {% set entries = craft.entries.relatedTo(primaryCategory) %}
{% endif %}
</code></pre>

<categories><relations>
How can I get the parent category group via the category to create multiple relation criteria?
# How can I get the parent category group via the category to create multiple relation criteria?

(Asked by: username_0 on 2015-12-03)

I have two category groups: Cars and Colors
My Cars category looks like this:
Cars
    Sports Cars
        Corvette
        Lamborghini
        Porche

My Colors category looks like this:
Colors
    Red
    Black
    White

I have two drop-down menus; one to choose the car, the other to choose the color.
I can choose the car, or the color just fine. My url's look like this:
mysite.com/cars/corvette/
- or - 
mysite.com/cars/red/

It's when I need to relate both categories. Since my two category groups have different levels, I'm not sure how to get the parent group of Colors from red. Which is what I think I need in order for the relatedTo to work properly.
For example, a url might look like this:
mysite.com/cars/corvette/  {# no problem #}  
- or - 
mysite.com/cars/red/       {# no problem #}

I'm able to get to the categories on my template:
{# 
 # Get the primary category GROUP via url segment. 
 # Example: Cars
 #
 #}
{% set primaryCategoryGroup = craft.categories.slug(craft.request.getSegment(2)).first() %}

{#
 # Get the primary CATEGORY via url segment.
 # Ex: Corvette or Red
 #}
{% set primaryCategory = craft.categories.slug(craft.request.getSegment(3)).first() %}

Then I check if I need to compare both relationships:
{% if craft.request.getSegment(4)|length %}
      {% set secondaryCategorySlug = craft.categories.slug(craft.request.getSegment(4)) %}
      {% set secondaryCategory = craft.categories.slug(craft.request.getSegment(4)).first() %}

      {{ secondaryCategory.title }}  {# Red or Corvette #}
      {{ primaryCategory }}  {# Red or Corvette #}

      {# Begin the fail #}
      {% set entries = craft.entries.section('mySection').relatedTo('and',
          { sourceElement: secondaryCategory, field:'title' },
          { targetElement: primaryCategory }
      ) %}
{% else %}
    {# Only get single relationship. #}
    {% set entries = craft.entries.relatedTo(primaryCategory) %}
{% endif %}

**Tags:** categories, relations


---

## 1 answers

### Answer by username_0 on 2015-12-03. Score: 0 | Accepted answer

Not 100% sure if this is correct, but this works.
Example urls:
www.mysite.com/cars/corvette/red
www.mysite.com/cars/red/corvette
...
www.mysite.com/cars/red/
www.mysite.com/cars/corvette

Here is my template:
{#
 # Check if there are multiple url segments to compare against.
 # This means we need to check against multiple relationships.
 # Ex: All Corvettes that are also Red
 #
 #}
{% if craft.request.getSegment(4)|length %}
    {% set secondaryCategory = craft.categories.slug(craft.request.getSegment(4)).first() %}
    {% set entries = craft.entries.section('mySection').relatedTo(['and', { targetElement: primaryCategory }, { targetElement: secondaryCategory }]) %}
{% else %}
    {#
     # Get all products related to the current category.
     # Ex: All Sports Cars that are related to Corvettes.
     #
     #}
    {% set entries = craft.entries.relatedTo(primaryCategory) %}
{% endif %}

...

{% if entries|length %}
    ...
{% endif %}

What i'm not sure about is both are using targetElement instead of one being sourceElement.


---

