<p>You’re close. But the <code>relatedTo</code> param can’t just be set to some username. It has to either be an element ID or an actual element model (more specifically, a user ID or UserModel in this case).</p>

<pre class="lang-php prettyprint-override"><code>'users/&lt;username:{handle}&gt;/offers.json' =&gt; function($username) {
    // Get the actual user model
    $criteria = craft()-&gt;elements-&gt;getCriteria('User');
    $criteria-&gt;username = $username;
    $user = $criteria-&gt;first();

    // If there was no user, return no results
    if (!$user) {
        return [
            'elementType' =&gt; 'Entry',
            'criteria' =&gt; ['id' =&gt; false],
        ];
    }

    return [
        'elementType' =&gt; 'Entry',
        'criteria' =&gt; [
            'section' =&gt; 'specialOffers',
            'relatedTo' =&gt; ['targetElement' =&gt; $user]
        ],
        // ...
    ];
},
</code></pre>

CC BY-SA 3.0
2016-04-11T14:34:20.657Z
Thank you!!!! Thank you so much!!! This was it! I didn't realize the relatedTo param had to be an element ID or an actual element model.
Sandy D.
CC BY-SA 3.0
2016-04-13T15:02:03.363Z
Is there any way to get the results of a matrix as well? I have a matrix block inside my Entry. I can query the first result on a matrix block in the entry by doing: $entry->mileage->first()->miles. However, I need to get it based on the User. Any suggestions?
Sandy D.
CC BY-SA 3.0
2016-04-11T14:06:35.777Z
2016-04-11T14:06:35.777Z
Brandon Kelly
<p>I have a channel called "offers".</p>

<p>My Offers channel contains a field called "Users".</p>

<p>The Users field is the "users" field type. It allows me to select 1 or more users that the Offer is for.</p>

<p>I want to get JSON output for this data based on the "Users" name. </p>

<pre><code>'users/&lt;username:{handle}&gt;/offers.json' =&gt; function($username) {
            return [
                'elementType' =&gt; 'Entry',
                'criteria' =&gt; ['section' =&gt; 'specialOffers', 'relatedTo' =&gt; $username],
            'transformer' =&gt; function(EntryModel $entry) {
                return [
                    'title' =&gt; $entry-&gt;title,
                    'category' =&gt; $entry-&gt;category-&gt;label,

                ];
              },
            ];
        },
</code></pre>

<plugin-elementapi>
How do I query data using the Element API Plugin based on a custom field?
# How do I query data using the Element API Plugin based on a custom field?

(Asked by: username_0 on 2016-04-06)

I have a channel called "offers".
My Offers channel contains a field called "Users".
The Users field is the "users" field type. It allows me to select 1 or more users that the Offer is for.
I want to get JSON output for this data based on the "Users" name. 
'users//offers.json' => function($username) {
            return [
                'elementType' => 'Entry',
                'criteria' => ['section' => 'specialOffers', 'relatedTo' => $username],
            'transformer' => function(EntryModel $entry) {
                return [
                    'title' => $entry->title,
                    'category' => $entry->category->label,

                ];
              },
            ];
        },

**Tags:** plugin-elementapi

---

## 1 answers

### Answer by username_1 on 2016-04-11. Score: 3 | Accepted answer

You’re close. But the relatedTo param can’t just be set to some username. It has to either be an element ID or an actual element model (more specifically, a user ID or UserModel in this case).
'users//offers.json' => function($username) {
    // Get the actual user model
    $criteria = craft()->elements->getCriteria('User');
    $criteria->username = $username;
    $user = $criteria->first();

    // If there was no user, return no results
    if (!$user) {
        return [
            'elementType' => 'Entry',
            'criteria' => ['id' => false],
        ];
    }

    return [
        'elementType' => 'Entry',
        'criteria' => [
            'section' => 'specialOffers',
            'relatedTo' => ['targetElement' => $user]
        ],
        // ...
    ];
},

> **Comments:**
> **username_0**: Thank you!!!! Thank you so much!!! This was it! I didn't realize the relatedTo param had to be an element ID or an actual element model.  
> **username_0**: Is there any way to get the results of a matrix as well? I have a matrix block inside my Entry. I can query the first result on a matrix block in the entry by doing: $entry->mileage->first()->miles. However, I need to get it based on the User. Any suggestions?  

---

