<p>I recommend doing it like so:</p>

<pre><code>{% set vacancies = craft.entries.section('vacancies').limit(null) %}

{% set catSlug = craft.request.segment(3) %}

{% if catSlug %}
    {% set cat = craft.entries.section('apprenticeships').slug(catSlug).first() %}

    {% if cat %}
        {% set vacancies = vacancies.relatedTo(cat) %}
    {% endif %}
{% endif %}
</code></pre>

<p>This sets up an ElementCriteriaModel for all your Vacancies entries (don't use the <code>find()</code> method yet!).
Then checks for the first entry from the Apprenticeships section with a slug of your URL segment.
If there's really such an entry, it further extends the ElementCriteriaModel with a <code>relatedTo</code> parameter.</p>

CC BY-SA 3.0
2015-10-08T15:40:30.360Z
I'm afraid this doesn't work either. In this case when a third segment is present it returns all the vacancies (related or not). When there is no third segment it shows nothing.
foamcow
CC BY-SA 3.0
2015-10-08T15:47:30.837Z
If I alter your code and use {% if catSlug %}
  {% set cat = craft.entries.slug(catSlug).first() %}
 {% endif %} then I get all vacancies when there is no third segment but nothing when there is a third segment.
foamcow
CC BY-SA 3.0
2015-10-08T15:47:40.120Z
and yes, I am on Craft Slack
foamcow
CC BY-SA 3.0
2015-10-08T16:15:31.553Z
This works perfectly. Part of the problem was my original code but the biggest issue was me testing with the wrong url!
foamcow
CC BY-SA 3.0
2015-10-08T14:25:48.767Z
2015-10-08T15:45:46.397Z
2015-10-08T15:45:46.397Z
carlcs
<p>I'm having trouble getting what should be a simple relationship between 2 channels to work as expected.</p>

<p>This is how things are set up.</p>

<p>Channel: Apprenticeships - essentially categories, types of apprenticeship (health, construction, catering etc)</p>

<p>Channel: Vacancies - individual apprenticeship courses  - each one related to an Apprenticehips entry.</p>

<p>In my template I can show a list of all Vacancy entries with </p>

<pre><code>{% set vacancies = craft.entries.section("vacancies").find() %}
</code></pre>

<p>Then loop through them and output field data. This works fine and shows all the vacancy entries.</p>

<p>But I want to be able to filter the entries returned to those that are related to a specific Apprenticeships entry. This filtering is to be based on the third url segment, which is the slug of the Apprenticeship entry.</p>

<pre><code>{% for cat in craft.entries.title(craft.entries.slug(craft.request.segment(3)).limit(1).find()) %}
</code></pre>

<p>This works fine and I can output field data from that Apprenticeship entry. Proving, in my mind, that Craft knows which entry I want.</p>

<p>To get only the Vacancy entries that are related to the Apprenticeship entry represented by cat I thought I could simply use 'cat' in another craft.entries tag.</p>

<pre><code>{% set vacancies = craft.entries.section("vacancies").relatedTo(cat) %}
</code></pre>

<p>Then loop through vacancies outputting the vacancy field data. But it doesn't return any Vacancy entries even though there definitely some to return.</p>

<p>Where am I going wrong here?</p>

<relations>
Problem getting relationship between entries to work
# Problem getting relationship between entries to work

(Asked by: username_0 on 2015-10-08)

I'm having trouble getting what should be a simple relationship between 2 channels to work as expected.
This is how things are set up.
Channel: Apprenticeships - essentially categories, types of apprenticeship (health, construction, catering etc)
Channel: Vacancies - individual apprenticeship courses  - each one related to an Apprenticehips entry.
In my template I can show a list of all Vacancy entries with 
{% set vacancies = craft.entries.section("vacancies").find() %}

Then loop through them and output field data. This works fine and shows all the vacancy entries.
But I want to be able to filter the entries returned to those that are related to a specific Apprenticeships entry. This filtering is to be based on the third url segment, which is the slug of the Apprenticeship entry.
{% for cat in craft.entries.title(craft.entries.slug(craft.request.segment(3)).limit(1).find()) %}

This works fine and I can output field data from that Apprenticeship entry. Proving, in my mind, that Craft knows which entry I want.
To get only the Vacancy entries that are related to the Apprenticeship entry represented by cat I thought I could simply use 'cat' in another craft.entries tag.
{% set vacancies = craft.entries.section("vacancies").relatedTo(cat) %}

Then loop through vacancies outputting the vacancy field data. But it doesn't return any Vacancy entries even though there definitely some to return.
Where am I going wrong here?

**Tags:** relations

---

## 1 answers

### Answer by username_1 on 2015-10-08. Score: 2 | Accepted answer

I recommend doing it like so:
{% set vacancies = craft.entries.section('vacancies').limit(null) %}

{% set catSlug = craft.request.segment(3) %}

{% if catSlug %}
    {% set cat = craft.entries.section('apprenticeships').slug(catSlug).first() %}

    {% if cat %}
        {% set vacancies = vacancies.relatedTo(cat) %}
    {% endif %}
{% endif %}

This sets up an ElementCriteriaModel for all your Vacancies entries (don't use the find() method yet!).
Then checks for the first entry from the Apprenticeships section with a slug of your URL segment.
If there's really such an entry, it further extends the ElementCriteriaModel with a relatedTo parameter.

> **Comments:**
> **username_0**: I'm afraid this doesn't work either. In this case when a third segment is present it returns all the vacancies (related or not). When there is no third segment it shows nothing.  
> **username_0**: If I alter your code and use {% if catSlug %}   {% set cat = craft.entries.slug(catSlug).first() %}  {% endif %} then I get all vacancies when there is no third segment but nothing when there is a third segment.  
> **username_0**: and yes, I am on Craft Slack  
> **username_0**: This works perfectly. Part of the problem was my original code but the biggest issue was me testing with the wrong url!  

---

