Users relatedTo multiple categories 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?
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 &gt; 1 %}
    {% set users = users.relatedTo(relationParam) %}
{% endif %}