1

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) %}

2 Answers 2

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!

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
}) %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.