<p>You can grab all ids and exclude them from your query</p>

<pre class="lang-twig prettyprint-override"><code>{% set excludeIds = craft.categories.group('services').relatedTo(entry).ids() %}
{% for category in craft.categories.where(['!=', 'elements.id', excludeIds]).all() %}
</code></pre>

<p>If your relation field is in your entry you can do</p>

<pre class="lang-twig prettyprint-override"><code>{% set categories = craft
    .categories
    .section('services')
    .where(['!=', 'elements.id', entry.category.ids()])
    .all()
%}
</code></pre>

CC BY-SA 4.0
2018-09-04T09:56:22.897Z
Thanks Robin. I'm still having a problem with this though, as it's now generating a database exception. This is what I have now:

    `<ul>
        {% set excludeIds = craft.categories.group('services').relatedTo(entry).ids() %}
        {% for category in craft.categories.where(['!=', 'elements.id', excludeIds]).all() %}
            <li>{{ category.title }}</li>
        {% endfor %}
    </ul>`
Eboon
CC BY-SA 4.0
2018-09-04T07:32:09.730Z
2018-09-04T07:32:09.730Z
Robin Schambach
<p>I'm trying to output a list of categories that are <strong>not</strong> related to a particular entry (in order to show a list of services other than the service related to the current entry).</p>

<p>I have this code which lists the category that <strong>is</strong> related to the current entry:</p>

<pre><code>&lt;ul&gt;
  {% set categories = craft.categories.group('services').relatedTo(entry).all() %}
  {% nav category in categories %}
    &lt;li&gt;{{ category.title }}&lt;/li&gt;
  {% endnav %}
&lt;/ul&gt;
</code></pre>

<p>Is there a way of turning this on its head, so that the other, non-related categories are displayed? Something like a <code>notRelatedTo(entry)</code> parameter?</p>

<craft3><categories><relatedto>
Display categories not related to an entry
# Display categories not related to an entry

(Asked by: username_0 on 2018-09-03)

I'm trying to output a list of categories that are not related to a particular entry (in order to show a list of services other than the service related to the current entry).
I have this code which lists the category that is related to the current entry:

  {% set categories = craft.categories.group('services').relatedTo(entry).all() %}
  {% nav category in categories %}
    {{ category.title }}
  {% endnav %}


Is there a way of turning this on its head, so that the other, non-related categories are displayed? Something like a notRelatedTo(entry) parameter?

**Tags:** craft3, categories, relatedto

---

## 1 answers

### Answer by username_1 on 2018-09-04. Score: 1

You can grab all ids and exclude them from your query
{% set excludeIds = craft.categories.group('services').relatedTo(entry).ids() %}
{% for category in craft.categories.where(['!=', 'elements.id', excludeIds]).all() %}

If your relation field is in your entry you can do
{% set categories = craft
    .categories
    .section('services')
    .where(['!=', 'elements.id', entry.category.ids()])
    .all()
%}

> **Comments:**
> **username_0**: Thanks Robin. I'm still having a problem with this though, as it's now generating a database exception. This is what I have now:      `<ul>         {% set excludeIds = craft.categories.group('services').relatedTo(entry).ids() %}         {% for category in craft.categories.where(['!=', 'elements.id', excludeIds]).all() %}             <li>{{ category.title }}</li>         {% endfor %}     </ul>`  

---

