<p>If you take a look at the <a href="https://craftcms.com/docs/relations" rel="nofollow noreferrer">Relations</a> page from the docs. The last example at the bottom of the page shows how you can do a query using <code>AND</code> for multiple criteria.</p>

<p>The example from the page:</p>

<blockquote>
  <p><strong>Passing Multiple Relation Criteria</strong></p>
  
  <p>There might be times when you need to factor multiple types of
  relations into the mix. For example, outputting all of the current
  user’s favorite cocktails that include Gin:</p>

<pre><code>{% set gin = craft.entries.section('ingredients').slug('gin').first() %}

{% set cocktails = craft.entries.section('cocktails').relatedTo('and',
    { sourceElement: currentUser, field: 'favoriteCocktails' },
    { targetElement: gin, field: 'ingredients' }
) %}
</code></pre>
  
  <p>That first argument ('and') specified that all of the relation
  criteria must be matched. You can pass 'or' instead if you want any of
  the relation criteria to match.</p>
</blockquote>

<p>Hopefully that'll point you in the right direction.</p>

CC BY-SA 3.0
2017-08-08T08:23:08.723Z
Great thanks Steve! In light of this I've edited my question with the answer.
Dazza
CC BY-SA 3.0
2017-08-08T06:47:21.810Z
2017-08-08T06:47:21.810Z
Steve Holland
<p>I have channel X set up where it's entries have a "users field", an "entries field" (that are picked from channel Y) and a whole bunch of other data with regards to the how a defined user has interacted with the defined entry.</p>

<p>Whilst viewing an entry from channel Y I wish to dig into the data kept in channel X, more specifically the entry in channel X that has both the same users field as the currentUser AND the same entries field as the entry viewed.</p>

<p>My current solution is the following and makes me cringe with the thought of things getting bigger...</p>

<pre><code>{% set fetchedResults = craft.entries.section('channelXhandle').relatedTo(currentUser) %}
{% for resultEntry in fetchedResults %}
    {% if resultEntry.member.first().id == currentUser.id %}
        {# grab the fruit #}
    {% endif %}
{% endfor %}
</code></pre>

<p>It's done the trick up until now, but now I want to find out if there is an entry in channel X that has both the channel Y entry and currentUser defined together.</p>

<p>I've tried the following but it didn't help. It retrieves all the entries as though the relatedTo params are <code>currentUser</code> or <code>entry</code> rather than <code>currentUser</code> and <code>entry</code> that I'm looking to retrieve.</p>

<pre><code>{% set fetchedResults = craft.entries.section('channelXhandle').relatedTo(currentUser, entry) %}
</code></pre>

<p>Any pointers in the right direction much appreciated!</p>

<h2>Solution in light of Steve's Answer</h2>

<pre><code>{% set fetchedResult = craft.entries.section('channelXhandle').relatedTo('and',
    { sourceElement: currentUser, field: '[users field handle]' },
    { targetElement: entry, field: '[entries field handle]' }
).first() %}
</code></pre>

<p>Note, I tagged on <code>.first()</code> in the above since in my case there should only be one resulting match anyway.</p>

<templating><relations><relatedto>
relatedTo currentUser AND entry
# relatedTo currentUser AND entry

(Asked by: username_0 on 2017-08-07)

I have channel X set up where it's entries have a "users field", an "entries field" (that are picked from channel Y) and a whole bunch of other data with regards to the how a defined user has interacted with the defined entry.
Whilst viewing an entry from channel Y I wish to dig into the data kept in channel X, more specifically the entry in channel X that has both the same users field as the currentUser AND the same entries field as the entry viewed.
My current solution is the following and makes me cringe with the thought of things getting bigger...
{% set fetchedResults = craft.entries.section('channelXhandle').relatedTo(currentUser) %}
{% for resultEntry in fetchedResults %}
    {% if resultEntry.member.first().id == currentUser.id %}
        {# grab the fruit #}
    {% endif %}
{% endfor %}

It's done the trick up until now, but now I want to find out if there is an entry in channel X that has both the channel Y entry and currentUser defined together.
I've tried the following but it didn't help. It retrieves all the entries as though the relatedTo params are currentUser or entry rather than currentUser and entry that I'm looking to retrieve.
{% set fetchedResults = craft.entries.section('channelXhandle').relatedTo(currentUser, entry) %}

Any pointers in the right direction much appreciated!
Solution in light of Steve's Answer
{% set fetchedResult = craft.entries.section('channelXhandle').relatedTo('and',
    { sourceElement: currentUser, field: '[users field handle]' },
    { targetElement: entry, field: '[entries field handle]' }
).first() %}

Note, I tagged on .first() in the above since in my case there should only be one resulting match anyway.

**Tags:** templating, relations, relatedto


---

## 1 answers

### Answer by username_1 on 2017-08-08. Score: 1 | Accepted answer

If you take a look at the Relations page from the docs. The last example at the bottom of the page shows how you can do a query using AND for multiple criteria.
The example from the page:

Passing Multiple Relation Criteria
There might be times when you need to factor multiple types of
  relations into the mix. For example, outputting all of the current
  user’s favorite cocktails that include Gin:
{% set gin = craft.entries.section('ingredients').slug('gin').first() %}

{% set cocktails = craft.entries.section('cocktails').relatedTo('and',
    { sourceElement: currentUser, field: 'favoriteCocktails' },
    { targetElement: gin, field: 'ingredients' }
) %}

That first argument ('and') specified that all of the relation
  criteria must be matched. You can pass 'or' instead if you want any of
  the relation criteria to match.

Hopefully that'll point you in the right direction.

> **Comments:**
> **username_0**: Great thanks Steve! In light of this I've edited my question with the answer.  

---

