<p>You would want to use the relatedTo() method of <code>elementCriteriaModel</code>.</p>

<pre><code>{% set username = craft.request.segment(2) %}
{% set author = craft.users.username(username).first() %}
{% set entries = craft.entries.section('blog').relatedTo(author) %}
</code></pre>

<p>If you need both then you may need to merge the results.</p>

<pre><code>{% set username = craft.request.segment(2) %}
{% set author = craft.users.username(username).first() %}
{% set authorEntries = craft.entries.section('blog').authorId(author.id).find() %}
{% set coAuthorEntries = craft.entries.section('blog').relatedTo(author).find() %}

{# merge authorEntries and coAuthorEntries #}
{% set entries = authorEntries|merge(coAuthorEntries) %}
</code></pre>

<p>Not tested. But should be close. Update: added <code>.find()</code> which will convert the elementCriteriaModel to an array so that the merge filter works properly.</p>

CC BY-SA 3.0
2014-12-16T21:55:15.497Z
2014-12-16T22:12:40.900Z
2014-12-16T22:12:40.900Z
Douglas McDonald
<p>In my template I have the user id variable pulled out of the URL and then I want to search through my entries for posts where that user id is included in a custom field with a users field type as well as the default "author" field.</p>

<p>Here's what I've got working for the author field, and I just want to include my custom "coAuthors" field type as well...</p>

<p>With a url like "/author/joyce"</p>

<pre><code>{% set username = craft.request.segment(2) %}
{% set author = craft.users.username(username).first() %}
{% set entries = craft.entries.section('blog').authorId(author.id) %}
</code></pre>

<p>That works great, but how can I also include entries where that author id is set in the coAuthors custom field?</p>

<p>I can't even seem to find it separately. This turns up nothing</p>

<pre><code>{% set entries = craft.entries.section('blog').search('coAuthors.username:author.id') %}
</code></pre>

<users><search>
How can I search for a user id in a custom field in my entries loop?
# How can I search for a user id in a custom field in my entries loop?

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

In my template I have the user id variable pulled out of the URL and then I want to search through my entries for posts where that user id is included in a custom field with a users field type as well as the default "author" field.
Here's what I've got working for the author field, and I just want to include my custom "coAuthors" field type as well...
With a url like "/author/joyce"
{% set username = craft.request.segment(2) %}
{% set author = craft.users.username(username).first() %}
{% set entries = craft.entries.section('blog').authorId(author.id) %}

That works great, but how can I also include entries where that author id is set in the coAuthors custom field?
I can't even seem to find it separately. This turns up nothing
{% set entries = craft.entries.section('blog').search('coAuthors.username:author.id') %}

**Tags:** users, search


---

## 1 answers

### Answer by username_1 on 2014-12-16. Score: 1 | Accepted answer

You would want to use the relatedTo() method of elementCriteriaModel.
{% set username = craft.request.segment(2) %}
{% set author = craft.users.username(username).first() %}
{% set entries = craft.entries.section('blog').relatedTo(author) %}

If you need both then you may need to merge the results.
{% set username = craft.request.segment(2) %}
{% set author = craft.users.username(username).first() %}
{% set authorEntries = craft.entries.section('blog').authorId(author.id).find() %}
{% set coAuthorEntries = craft.entries.section('blog').relatedTo(author).find() %}

{# merge authorEntries and coAuthorEntries #}
{% set entries = authorEntries|merge(coAuthorEntries) %}

Not tested. But should be close. Update: added .find() which will convert the elementCriteriaModel to an array so that the merge filter works properly.


---

