<p>You would first need to query for the users that match your criteria and than query for the profile entries which are related to one of these users.</p>

<pre><code>{% set userIds = craft.users({
    year: craft.request.getSegment(3),
    userOptions: craft.request.getSegment(2),
}).ids() %}

{% set profiles = craft.entries({
    section: 'profiles',
    relatedTo: {
        targetElement: userIds,
        field: 'userAccount',
    },
    with: ['userAccount'],
}) %}
</code></pre>

CC BY-SA 3.0
2017-03-10T15:31:33.023Z
Ha, beat me to it!
Josh Angell
CC BY-SA 3.0
2017-03-10T15:32:18.497Z
Note that `where` isn’t a valid param, the [`relatedTo`](https://craftcms.com/docs/relations) one is the key.
Josh Angell
CC BY-SA 3.0
2017-03-10T15:30:57.840Z
2017-03-10T15:30:57.840Z
carlcs
<p>I am trying query against URL segments. Where I am hung up is how to pass those arguments to a related field.</p>

<p>For example, here is my current query:</p>

<p><strong>twig</strong></p>

<pre><code>{% set profiles = craft.entries({
    section: 'profiles',
    with: ['userAccount']
}) %}
</code></pre>

<p><code>userAccount</code> is a user field that is related back to the user. </p>

<p>I get all entries along with the user account information &amp; all is well.</p>

<p>I would like to query <em>against</em> the <code>userAcount</code> field to only return the appropriate entries in the <code>profile</code> section given url segments.</p>

<p>For example: <code>www.mysite.com/profiles/foo/2017</code></p>

<p>From the <code>userAccount</code> field:</p>

<p><code>foo</code> is a checkbox field (name is <code>userOptions</code>. Options are: <code>foo</code>, <code>bar</code>, <code>baz</code>)</p>

<p>`2017 is a plain text field.</p>

<p>This is what I am trying to accomplish (obviously is not correct)</p>

<pre><code>{% set profiles = craft.entries({
    section: 'profiles',
    with: ['userAccount'],
    where: ['userAccount[0].year', '=', craft.request.segment('3')],
    where: ['userAccount[0].userOptions', '=', craft.request.segment('2')],
   limit: null
}) %}
</code></pre>

<p>I am getting entries bak when I only query against the year. I haven't checked if they are <em>correct</em> but at least I get something. Where I'm thrown is since <code>userOptions</code> could be any value, how can I check that field if a selected option matches the url segment? Any user could have <code>foo</code> <em>and</em> <code>bar</code> selected. I think I need something like a <code>WHERE IN</code> or something...</p>

<p>Thank you for any suggestions!</p>

<templating><queries>
How can I use a WHERE statement in my template?
# How can I use a WHERE statement in my template?

(Asked by: username_0 on 2017-03-10)

I am trying query against URL segments. Where I am hung up is how to pass those arguments to a related field.
For example, here is my current query:
twig
{% set profiles = craft.entries({
    section: 'profiles',
    with: ['userAccount']
}) %}

userAccount is a user field that is related back to the user. 
I get all entries along with the user account information & all is well.
I would like to query against the userAcount field to only return the appropriate entries in the profile section given url segments.
For example: www.mysite.com/profiles/foo/2017
From the userAccount field:
foo is a checkbox field (name is userOptions. Options are: foo, bar, baz)
`2017 is a plain text field.
This is what I am trying to accomplish (obviously is not correct)
{% set profiles = craft.entries({
    section: 'profiles',
    with: ['userAccount'],
    where: ['userAccount[0].year', '=', craft.request.segment('3')],
    where: ['userAccount[0].userOptions', '=', craft.request.segment('2')],
   limit: null
}) %}

I am getting entries bak when I only query against the year. I haven't checked if they are correct but at least I get something. Where I'm thrown is since userOptions could be any value, how can I check that field if a selected option matches the url segment? Any user could have foo and bar selected. I think I need something like a WHERE IN or something...
Thank you for any suggestions!

**Tags:** templating, queries


---

## 1 answers

### Answer by username_1 on 2017-03-10. Score: 4 | Accepted answer

You would first need to query for the users that match your criteria and than query for the profile entries which are related to one of these users.
{% set userIds = craft.users({
    year: craft.request.getSegment(3),
    userOptions: craft.request.getSegment(2),
}).ids() %}

{% set profiles = craft.entries({
    section: 'profiles',
    relatedTo: {
        targetElement: userIds,
        field: 'userAccount',
    },
    with: ['userAccount'],
}) %}

> **Comments:**
> **username_2**: Ha, beat me to it!  
> **username_2**: Note that `where` isn’t a valid param, the [`relatedTo`](https://craftcms.com/docs/relations) one is the key.  

---

