<p>Try passing your <code>relatedTo</code> parameters as an array:</p>

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

<p>or a little easier to read:</p>

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

<p><strong>Edit:</strong></p>

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

<pre><code>{% 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 }
    ]
}) %}
</code></pre>

<p><strong>Update:</strong><br>
<em>(Copied the solution from Sean's <a href="https://craftcms.stackexchange.com/questions/5105/searching-members-fields">other question</a>, because it also fits here!)</em></p>

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

<pre><code>{# 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 %}
</code></pre>

CC BY-SA 3.0
2014-12-05T11:44:49.293Z
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
CC BY-SA 3.0
2014-12-05T11:46:36.427Z
Yes, will update my answer. Glad that passing the params as an array works!
carlcs
CC BY-SA 3.0
2014-12-05T11:54:03.420Z
Okay, I can see how this will work but there is an extra step I'll need help with...Another question...
Sean Delaney
CC BY-SA 3.0
2014-12-05T11:54:41.247Z
http://craftcms.stackexchange.com/questions/5105/searching-members-fields
Sean Delaney
CC BY-SA 3.0
2014-12-05T12:00:59.253Z
Are you able to help with the linked question also?
Sean Delaney
CC BY-SA 3.0
2014-12-19T08:28:08.970Z
@carlcs I think I may have spotted an error in that last block. I believe `relationParam|merge({ targetElement: service1 })` should be wrapped in brackets like so `relationParam|merge([{ targetElement: service1 }])`. I noticed it when adapting this for someone else's question.
Douglas McDonald
CC BY-SA 3.0
2014-12-05T11:24:51.680Z
2015-02-13T15:52:34.763Z
2017-04-13T12:51:00.200Z
carlcs
<p>Trying to get users in a members group related to multiple categories.</p>

<pre><code>{% 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 }}
</code></pre>

<p>Number of members returned is zero.</p>

<p>Any ideas?</p>

CC BY-SA 3.0
2014-12-05T11:33:49.183Z
Hmm... this looks right to me, but you could try passing your `relatedTo` parameters as an array, Sean.
carlcs
<users><categories><relations>
Users relatedTo multiple categories
# Users relatedTo multiple categories

(Asked by: username_0 on 2014-12-05)

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?

**Tags:** users, categories, relations
> **Comments:**
> **username_1**: Hmm... this looks right to me, but you could try passing your `relatedTo` parameters as an array, Sean.  

---

## 1 answers

### Answer by username_1 on 2014-12-05. Score: 23 | Accepted answer

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

> **Comments:**
> **username_0**: 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') %} ?  
> **username_1**: Yes, will update my answer. Glad that passing the params as an array works!  
> **username_0**: Okay, I can see how this will work but there is an extra step I'll need help with...Another question...  
> **username_0**: http://craftcms.stackexchange.com/questions/5105/searching-members-fields  
> **username_0**: Are you able to help with the linked question also?  
> **username_2**: @carlcs I think I may have spotted an error in that last block. I believe `relationParam|merge({ targetElement: service1 })` should be wrapped in brackets like so `relationParam|merge([{ targetElement: service1 }])`. I noticed it when adapting this for someone else's question.  

---

