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?

  • Hmm... this looks right to me, but you could try passing your relatedTo parameters as an array, Sean. – carlcs Dec 5 '14 at 11:33
up vote 14 down vote accepted

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 %}
  • 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') %} ? – Sean Delaney Dec 5 '14 at 11:44
  • Yes, will update my answer. Glad that passing the params as an array works! – carlcs Dec 5 '14 at 11:46
  • Okay, I can see how this will work but there is an extra step I'll need help with...Another question... – Sean Delaney Dec 5 '14 at 11:54
  • Are you able to help with the linked question also? – Sean Delaney Dec 5 '14 at 12:00

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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