<p>I wouldn't want to rely on up-to-date search indexes for this and would rather make use of the <code>relatedTo</code> parameter instead.</p>

<p>Unfortunatelly it isn't very straightforward to exclude a category (e.g. <code>relatedTo('not 24')</code> doesn't work), but there's a common workaround
many Craft devs use in cases like this: collect IDs and query against them (→ see related questions: <a href="https://craftcms.stackexchange.com/a/10479/125">Get entries NOT related to categories</a> or
<a href="https://craftcms.stackexchange.com/a/4930/125">How can I select users that are not members of any user groups?</a>).</p>

<pre><code>{% set leadCategory = craft.categories.group('staffHierarchy').slug('leadership').first() %}
{% set studCategory = craft.categories.group('staffHierarchy').slug('student').first() %}

{% set alumniCategory = craft.categories.group('staffHierarchy').slug('alumni').first() %}

{% set alumniGroupIds = craft.entries({
    section: 'people',
    relatedTo: alumniCategory,
    limit: null
}).ids() %}

{% set idParam = 'and, not ' ~ alumniGroupIds|join(', not ') %}

{% set leadGroup = craft.entries({
    section: 'people',
    id: idParam,
    relatedTo: leadCategory,
    limit: null
}) %}

{% set studGroup = craft.entries({
    section: 'people',
    id: idParam,
    relatedTo: studCategory,
    limit: null
}) %}
</code></pre>

CC BY-SA 3.0
2016-01-13T11:12:18.577Z
2016-01-13T11:37:03.270Z
2017-04-13T12:51:00.200Z
carlcs
<p>The <a href="https://craftcms.com/docs/templating/craft.entries#search" rel="nofollow">search parameter</a> will help you do this really elegantly.</p>

<p>In the code below I've called the category field <code>categoryField</code>, but you'll have to swap that to suit your site.</p>

<pre><code>{# Get current students (students who aren't also in the alumni category) #}
{% set currentStudents = craft.entries.section('people').search('categoryField:student -categoryField:alumni') %}

{% for student in currentStudents %}
    {# Output your students here #}
{% endfor %}
</code></pre>

<p>This is basically asking Craft to respond with the entries that have the categoryField including student but not (indicated by the <code>-</code> before) including alumni.</p>

<p>You could probably also streamline the other bits of your code using this method.</p>

<p>I've tested this as working on Craft 2.5.2757</p>

<p>Hope that helps!</p>

CC BY-SA 3.0
2016-01-09T16:24:29.613Z
2016-01-13T08:39:55.637Z
2016-01-13T08:39:55.637Z
Mutual
<p>Right now, I have a staff profile page. Each person on the page has a category association (professional, leadership, student). When the students graduate, I want to add an 'alumni' category to their entry, and have them stop showing up on the active page. Right now I have student and alumni associated with the particular entry, and I want to hide all entries with the alumni category. Any idea how to go about this?</p>

<pre><code>{% set leaderGroup = craft.categories.group('staffHierarchy').slug('leadership') %}
{% set leader = craft.entries.section('people').relatedTo(leaderGroup) %}
{% set staffGroup = craft.categories.group('staffHierarchy').slug('student') %}
{% set staff = craft.entries.section('people').relatedTo(staffGroup) %}
</code></pre>

<categories><entry><conditional><category>
How can I hide entries depending on which category they are associated with?
# How can I hide entries depending on which category they are associated with?

(Asked by: username_0 on 2016-01-08)

Right now, I have a staff profile page. Each person on the page has a category association (professional, leadership, student). When the students graduate, I want to add an 'alumni' category to their entry, and have them stop showing up on the active page. Right now I have student and alumni associated with the particular entry, and I want to hide all entries with the alumni category. Any idea how to go about this?
{% set leaderGroup = craft.categories.group('staffHierarchy').slug('leadership') %}
{% set leader = craft.entries.section('people').relatedTo(leaderGroup) %}
{% set staffGroup = craft.categories.group('staffHierarchy').slug('student') %}
{% set staff = craft.entries.section('people').relatedTo(staffGroup) %}

**Tags:** categories, entry, conditional, category

---

## 2 answers

### Answer by username_1 on 2016-01-13. Score: 2

I wouldn't want to rely on up-to-date search indexes for this and would rather make use of the relatedTo parameter instead.
Unfortunatelly it isn't very straightforward to exclude a category (e.g. relatedTo('not 24') doesn't work), but there's a common workaround
many Craft devs use in cases like this: collect IDs and query against them (→ see related questions: Get entries NOT related to categories or
How can I select users that are not members of any user groups?).
{% set leadCategory = craft.categories.group('staffHierarchy').slug('leadership').first() %}
{% set studCategory = craft.categories.group('staffHierarchy').slug('student').first() %}

{% set alumniCategory = craft.categories.group('staffHierarchy').slug('alumni').first() %}

{% set alumniGroupIds = craft.entries({
    section: 'people',
    relatedTo: alumniCategory,
    limit: null
}).ids() %}

{% set idParam = 'and, not ' ~ alumniGroupIds|join(', not ') %}

{% set leadGroup = craft.entries({
    section: 'people',
    id: idParam,
    relatedTo: leadCategory,
    limit: null
}) %}

{% set studGroup = craft.entries({
    section: 'people',
    id: idParam,
    relatedTo: studCategory,
    limit: null
}) %}


---

### Answer by username_2 on 2016-01-09. Score: 2

The search parameter will help you do this really elegantly.
In the code below I've called the category field categoryField, but you'll have to swap that to suit your site.
{# Get current students (students who aren't also in the alumni category) #}
{% set currentStudents = craft.entries.section('people').search('categoryField:student -categoryField:alumni') %}

{% for student in currentStudents %}
    {# Output your students here #}
{% endfor %}

This is basically asking Craft to respond with the entries that have the categoryField including student but not (indicated by the - before) including alumni.
You could probably also streamline the other bits of your code using this method.
I've tested this as working on Craft 2.5.2757
Hope that helps!


---

