<p>So I solved this with the following code:</p>

<pre><code>  {# 
   // loop through related photos
   // and loop through related category photos
   // set photoId = to common id in both loops
  #}
  {% set photoId = "" %}
  {% for photo in photos %}
    {% for catPhoto in photos.relatedTo(currentCategory.id) %}
      {% if photo.id == catPhoto.id %}

        {% set photoId = catPhoto.id %}
        {# {% set photoId = photoId|merge([catPhoto.id]) %} #}
      {% endif %}
    {% endfor %}
  {% endfor %}
</code></pre>

<p>Then the following outputs the entry in question</p>

<pre><code>{% for photo in craft.entries.section('photos').id(photoId).find() %}
  // output entry related to photographer and category here
{% endfor %}
</code></pre>

CC BY-SA 3.0
2018-01-19T19:16:13.073Z
2018-01-19T19:16:13.073Z
CreateSean
<p>We’ve built a plugin called <a href="https://github.com/miranj/craft-router" rel="nofollow noreferrer">Router</a> that solves exactly this use case.</p>

<p>For the URL structure you described – <code>domain.com/photographers/john-doe/food</code> – this can be solved by the following configuration for the Router plugin:</p>

<pre class="lang-php prettyprint-override"><code>/* config/router.php */

return [
  'rules' =&gt; [

    // URI pattern with named subpatterns
    '&lt;placeholder:photographers&gt;/&lt;photographerEntry:{slug}&gt;/&lt;foodCategory:{slug}&gt;' =&gt; [

      // array of filters that are activated when
      // the key matches a subpattern variable declared in
      // the route's regular expression
      'criteria' =&gt; [
        'placeholder' =&gt; 'section:photographerPhotos', // Base section
        'photographerEntry' =&gt; 'entry:photographers',  // Find slug in section        
        'foodCategory' =&gt; 'category:food',  // Find slug in group
      ],

      // template file
      'template' =&gt; 'photographerPhotos/_entry',
    ],

  ]
];
</code></pre>

<p>And in the template:</p>

<pre><code>{# photographerPhotos/_entry #}

{{ entries }} {# array of photos (related to both the photographer and food category) #}
{{ photographerEntry }} {# photographer Entry object #}
{{ foodCategory }} {# food Category object #}
</code></pre>

CC BY-SA 4.0
2019-07-24T13:19:03.727Z
2019-08-01T13:08:56.907Z
2019-08-01T13:08:56.907Z
rungta
<p>Channels:
  Photographers
  Photos - entries field handle "photographers"
         - categories field handle "category"</p>

<p>url structure where segment 2 is the photographer slug and segment 3 is the photo category slug:</p>

<p>domain.com/photographers/john-doe/food</p>

<p>On the photographers detail page I need to pull up the photos entries that are related to that photographer and in the category determined by segment 3</p>

<p>Set at the top of the page and used in a couple of different spots</p>

<pre><code>{# set segment variables so we can use shorthand #}
{% set segment_2 = craft.request.getSegment(2) %}
{% set segment_3 = craft.request.getSegment(3) %}

{# get related photo entries #}
{% set photos = craft.entries.section('photos').relatedTo(entry) %}
</code></pre>

<p>Then where I want the photos to display I have the following code which outputs 2 entries from the photos channel. These are both related to the current photographer but in different categories. Only the one entry that is both related to the photographer and the current category should be output.</p>

<pre><code>  {# set currentCategory to category id from segment 3 #}
  {% set currentCategory = craft.categories.slug(segment_3).first() %}

  {% set photos = photos.relatedTo(currentCategory.id) %}

  {% for photo in photos %}
    {{ photo.title }} &lt;br&gt;
  {%endfor %}
</code></pre>

<p>I'm not sure how to combine and filter these relations. Please advise.</p>

<templating><entries><categories><relatedto>
Get Related Entries and then filter those by specific category in url segment
# Get Related Entries and then filter those by specific category in url segment

(Asked by: username_0 on 2018-01-19)

Channels:
  Photographers
  Photos - entries field handle "photographers"
         - categories field handle "category"
url structure where segment 2 is the photographer slug and segment 3 is the photo category slug:
domain.com/photographers/john-doe/food
On the photographers detail page I need to pull up the photos entries that are related to that photographer and in the category determined by segment 3
Set at the top of the page and used in a couple of different spots
{# set segment variables so we can use shorthand #}
{% set segment_2 = craft.request.getSegment(2) %}
{% set segment_3 = craft.request.getSegment(3) %}

{# get related photo entries #}
{% set photos = craft.entries.section('photos').relatedTo(entry) %}

Then where I want the photos to display I have the following code which outputs 2 entries from the photos channel. These are both related to the current photographer but in different categories. Only the one entry that is both related to the photographer and the current category should be output.
  {# set currentCategory to category id from segment 3 #}
  {% set currentCategory = craft.categories.slug(segment_3).first() %}

  {% set photos = photos.relatedTo(currentCategory.id) %}

  {% for photo in photos %}
    {{ photo.title }} 
  {%endfor %}

I'm not sure how to combine and filter these relations. Please advise.

**Tags:** templating, entries, categories, relatedto


---

## 2 answers

### Answer by username_0 on 2018-01-19. Score: 1 | Accepted answer

So I solved this with the following code:
  {# 
   // loop through related photos
   // and loop through related category photos
   // set photoId = to common id in both loops
  #}
  {% set photoId = "" %}
  {% for photo in photos %}
    {% for catPhoto in photos.relatedTo(currentCategory.id) %}
      {% if photo.id == catPhoto.id %}

        {% set photoId = catPhoto.id %}
        {# {% set photoId = photoId|merge([catPhoto.id]) %} #}
      {% endif %}
    {% endfor %}
  {% endfor %}

Then the following outputs the entry in question
{% for photo in craft.entries.section('photos').id(photoId).find() %}
  // output entry related to photographer and category here
{% endfor %}


---

### Answer by username_1 on 2019-07-24. Score: 2

We’ve built a plugin called Router that solves exactly this use case.
For the URL structure you described – domain.com/photographers/john-doe/food – this can be solved by the following configuration for the Router plugin:
/* config/router.php */

return [
  'rules' => [

    // URI pattern with named subpatterns
    '//' => [

      // array of filters that are activated when
      // the key matches a subpattern variable declared in
      // the route's regular expression
      'criteria' => [
        'placeholder' => 'section:photographerPhotos', // Base section
        'photographerEntry' => 'entry:photographers',  // Find slug in section        
        'foodCategory' => 'category:food',  // Find slug in group
      ],

      // template file
      'template' => 'photographerPhotos/_entry',
    ],

  ]
];

And in the template:
{# photographerPhotos/_entry #}

{{ entries }} {# array of photos (related to both the photographer and food category) #}
{{ photographerEntry }} {# photographer Entry object #}
{{ foodCategory }} {# food Category object #}


---

