3

I have a list of people with skills defined by a category group.

I have a category field within the 'people' Section so when a new person (entry) is created, the category field allows the admin to assign appropriate skills to that new profile.

I have a series of category entries for the 'skills' available.

When a user visits an "Our Skills" entry page, I need to display the title (name) of the people who have had that particular skill assigned to their profile via the category field.

I think this is reverse relations but I can't get my head around it?

Would appreciate some help.

Many thanks

Martin

1 Answer 1

1

Yes, you're right, one could call this a reverse-relation. The field you created the relation from is the "people" entry and you're looking at it from the category element's page.

To describe this direction in Craft relatedTo parameter, you define whether the element you're dealing with is the sourceElement of that relation, which means it is the element with the field, or if it is a targetElement, one of the elements selected in the field.

So on your "Our Skills" page you have a category element that got selected from a categories field, this makes it a targetElement. I'm also setting that categories field's handle here.

{% set individuals = craft.entries({
    section: 'people',
    relatedTo: {
        targetElement: category,
        field: 'mySkillsFieldHandle'
    }
}) %}

In most cases you could just use the element property, because there are no other relations between both elements. But I actually like to always define my relations precisely, because who knows what we add to the site in the future, that potentially breaks loose definitions.

0

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.