9

Trying to get users in a members group related to multiple categories.

{% set services = craft.categories.group('services').slug('service-1, service-2') %}
{% set locations = craft.categories.group('locations').slug('location-1') %}

{% set members = craft.users.group("members").relatedTo("and", { targetElement: services }, { targetElement: locations } ) %}

{{ members | length }}

Number of members returned is zero.

Any ideas?

1
  • Hmm... this looks right to me, but you could try passing your relatedTo parameters as an array, Sean.
    – carlcs
    Dec 5, 2014 at 11:33

1 Answer 1

21

Try passing your relatedTo parameters as an array:

{% set members = craft.users.group('members').relatedTo([ 'and', { targetElement: services }, { targetElement: locations } ]) %}

or a little easier to read:

{% set entries = craft.users({
    group: 'members',
    relatedTo: [
        'and',
        { targetElement: services },
        { targetElement: locations }
    ]
}) %}

Edit:

If you want to get only those users that are related to both services, add another relation to your parameter array:

{% set service1 = craft.categories.group('services').slug('service-1') %}
{% set service2 = craft.categories.group('services').slug('service-2') %}
{% set locations = craft.categories.group('locations').slug('location-1') %}

{% set entries = craft.users({
    group: 'members',
    relatedTo: [
        'and',
        { targetElement: service1 },
        { targetElement: service2 },
        { targetElement: locations }
    ]
}) %}

Update:
(Copied the solution from Sean's other question, because it also fits here!)

If you need to add those relations programatically, that's how I would do it:

{# Compose the relation param #}
{% set relationParam = ['and'] %}

{# Add to the relation param #}
{% if condition1 %}
    {% set service1 = craft.categories.group('services').slug('service-1') %}
    {% set relationParam = relationParam|merge([{ targetElement: service1 }]) %}
{% endif %}

{% if condition2 %}
    {% set service2 = craft.categories.group('services').slug('service-2') %}
    {% set relationParam = relationParam|merge([{ targetElement: service2 }]) %}
{% endif %}

{# Get users of group "member" #}
{% set users = craft.users.group('members') %}

{# Further filter users - avoid passing an array with `['and']` only #}
{% if relationParam|length > 1 %}
    {% set users = users.relatedTo(relationParam) %}
{% endif %}
6
  • This returns all members with either categories. It is possible to do something like: {% set services = craft.categories.group('services').slug('service-1 AND service-2') %} ? Dec 5, 2014 at 11:44
  • Yes, will update my answer. Glad that passing the params as an array works!
    – carlcs
    Dec 5, 2014 at 11:46
  • Okay, I can see how this will work but there is an extra step I'll need help with...Another question... Dec 5, 2014 at 11:54
  • Are you able to help with the linked question also? Dec 5, 2014 at 12:00

Your Answer

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

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