<p>Why don't you use your current entry as reference?</p>

<pre class="lang-twig prettyprint-override"><code>{% set nutritions = craft.entries.section('nutrition').relatedTo({
    field: 'restaurant',
    targetElement: entry
}).all() %}
</code></pre>

<p>Since your current entry is related to the entry you want to fetch this would be the besser solution. However that URL structure is not really optimal to beginn with. There are many cases it will break as soon as someone edits your entries, but filtering by slug alone is never a good idea in such a way.</p>

CC BY-SA 4.0
2019-03-12T22:45:31.207Z
That's because there are no entries created with slugs like restaurant-name-nutrition. The /nutrition/index.twig template lists restaurants that have nutritions, urls are generated automatically - restaurant-name and nutrition postfix which are handled via route for proper template based on this url format. I'd strongly avoid creating categories or entries that would have restaurant-name-nutrition slugs. I know it would be easier but it adds too much complexity.
Mark H.
CC BY-SA 4.0
2019-03-12T12:26:15.623Z
2019-03-12T12:26:15.623Z
Robin Schambach
<p>Channel: Restaurants</p>

<p>Channel: Nutrition / contains entries field named restaurant. </p>

<p>Required URL format:
    restaurant-name-nutrition</p>

<p>since the URL structure is merged I can't use getSegement(x) while getting entries.</p>

<p>in other scenario where the URL would be</p>

<p>nutrition/restaurant-name</p>

<p>I could use something like this</p>

<pre><code>{% set seg2 = craft.request.getSegment(2)
{% for entry in craft.entries.section('nutrition').slug(seg2) %}
</code></pre>

<p>but what is the right approach for that one?</p>

<url><segments>
Filter by slug from URL (advanced)
# Filter by slug from URL (advanced)

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

Channel: Restaurants
Channel: Nutrition / contains entries field named restaurant. 
Required URL format:
    restaurant-name-nutrition
since the URL structure is merged I can't use getSegement(x) while getting entries.
in other scenario where the URL would be
nutrition/restaurant-name
I could use something like this
{% set seg2 = craft.request.getSegment(2)
{% for entry in craft.entries.section('nutrition').slug(seg2) %}

but what is the right approach for that one?

**Tags:** url, segments


---

## 1 answers

### Answer by username_1 on 2019-03-12. Score: 1

Why don't you use your current entry as reference?
{% set nutritions = craft.entries.section('nutrition').relatedTo({
    field: 'restaurant',
    targetElement: entry
}).all() %}

Since your current entry is related to the entry you want to fetch this would be the besser solution. However that URL structure is not really optimal to beginn with. There are many cases it will break as soon as someone edits your entries, but filtering by slug alone is never a good idea in such a way.

> **Comments:**
> **username_0**: That's because there are no entries created with slugs like restaurant-name-nutrition. The /nutrition/index.twig template lists restaurants that have nutritions, urls are generated automatically - restaurant-name and nutrition postfix which are handled via route for proper template based on this url format. I'd strongly avoid creating categories or entries that would have restaurant-name-nutrition slugs. I know it would be easier but it adds too much complexity.  

---

