<p>You can do</p>

<pre><code>{% set ids = myGlobal.liveCategories.ids() %}
{% set category = craft.categories.slug(craft.request.getSegment(3)).id(ids).first() %} 
{% if category %} 
    The category is valid 
{% else %} 
    No slug found, or the category is not in the list 
{% endif %} 
</code></pre>

<p>Explanation: you grab all the ids from your allowed categories and search for them in your query. If a category is found it is one of the allowed categories. If not the category is another one or the there is no element with a matching slug. </p>

CC BY-SA 3.0
2018-01-21T16:55:15.137Z
Brilliant and does exactly what I needed. Thank you!
jrothafer
CC BY-SA 3.0
2018-01-21T08:05:54.707Z
2018-01-21T08:15:46.060Z
2018-01-21T08:15:46.060Z
Robin Schambach
<p>So I have a Commerce product template page that grabs products something like this (segment 3 is the category slug):</p>

<pre><code>{% set category = craft.categories.slug(craft.request.getSegment(3)).first() %}

{% set product = craft.commerce.products({
    relatedTo: category,
    status: 'live',
}).first() %}
</code></pre>

<p>However, I need this page to 404 if the category slug isn't in a collection of categories that are chosen in a global field:</p>

<pre><code>{% for category in myGlobal.liveCategories %}
    These categories, like this one {{ category.title }}, are the only categories that are allowed right now
{% endfor %}
</code></pre>

<p>So I want to filter </p>

<pre><code>{% set category = craft.categories.slug(craft.request.getSegment(3)).first() %}
</code></pre>

<p>against my global category field:</p>

<pre><code>myGlobal.liveCategories
</code></pre>

<p>so that only the <code>category</code> that gets passed to my product loop is a valid category in the global set. Here is what I'm trying to accomplish, but this code block doesn't work:</p>

<pre><code>{% set category = craft.categories.slug(craft.request.getSegment(3)).first() %}

{% if category not in myGlobal.liveCategories %}
    {% redirect 404 %}
{% endif %}
</code></pre>

<p>I assume I have to do some twig filtering, or create arrays and combine, ...or something. </p>

<templating>
Filtering category objects in a template?
# Filtering category objects in a template?

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

So I have a Commerce product template page that grabs products something like this (segment 3 is the category slug):
{% set category = craft.categories.slug(craft.request.getSegment(3)).first() %}

{% set product = craft.commerce.products({
    relatedTo: category,
    status: 'live',
}).first() %}

However, I need this page to 404 if the category slug isn't in a collection of categories that are chosen in a global field:
{% for category in myGlobal.liveCategories %}
    These categories, like this one {{ category.title }}, are the only categories that are allowed right now
{% endfor %}

So I want to filter 
{% set category = craft.categories.slug(craft.request.getSegment(3)).first() %}

against my global category field:
myGlobal.liveCategories

so that only the category that gets passed to my product loop is a valid category in the global set. Here is what I'm trying to accomplish, but this code block doesn't work:
{% set category = craft.categories.slug(craft.request.getSegment(3)).first() %}

{% if category not in myGlobal.liveCategories %}
    {% redirect 404 %}
{% endif %}

I assume I have to do some twig filtering, or create arrays and combine, ...or something.

**Tags:** templating

---

## 1 answers

### Answer by username_1 on 2018-01-21. Score: 1 | Accepted answer

You can do
{% set ids = myGlobal.liveCategories.ids() %}
{% set category = craft.categories.slug(craft.request.getSegment(3)).id(ids).first() %} 
{% if category %} 
    The category is valid 
{% else %} 
    No slug found, or the category is not in the list 
{% endif %} 

Explanation: you grab all the ids from your allowed categories and search for them in your query. If a category is found it is one of the allowed categories. If not the category is another one or the there is no element with a matching slug.

> **Comments:**
> **username_0**: Brilliant and does exactly what I needed. Thank you!  

---

