Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-08T15:40:30.360Z
Answers.Text: 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.
Answers.UserDisplayName: foamcow
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-08T15:47:30.837Z
Answers.Text: 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.
Answers.UserDisplayName: foamcow
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-08T15:47:40.120Z
Answers.Text: and yes, I am on Craft Slack
Answers.UserDisplayName: foamcow
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-08T16:15:31.553Z
Answers.Text: This works perfectly. Part of the problem was my original code but the biggest issue was me testing with the wrong url!
Answers.UserDisplayName: foamcow
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-08T14:25:48.767Z
Answers.LastActivityDate: 2015-10-08T15:45:46.397Z
Answers.LastEditDate: 2015-10-08T15:45:46.397Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <relations>
Title: Problem getting relationship between entries to work
ThreadText: # 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!  
  
  ---
  
  