<p>Completely untested, but something like this should work:</p>

<pre><code>'dealers/&lt;username:(.*)&gt;.json' =&gt; function($username) {
    $author = craft()-&gt;users-&gt;getUserByUsernameOrEmail($username);

    return [
        'elementType' =&gt; 'Entry',
        'criteria' =&gt; [
            'section' =&gt; 'listings',
            'authorId' =&gt; $author-&gt;id
        ],
        'paginate' =&gt; false,
        'transformer' =&gt; function(EntryModel $entry) {
            ...
</code></pre>

CC BY-SA 3.0
2017-05-22T13:51:06.800Z
While it didn't give me an error, this did not work. It just lists all entries within the 'listings' section, not filtering by username.
Noah Yamen
CC BY-SA 3.0
2017-05-22T14:59:14.353Z
Additional note: I do have a working feed that does filter listings via authorID. But that's by using the author ID in the feed URL. In this feed's case, it has to have the username of the user. Is there a way to get the ID of a user via their username, and then pass it to " 'authorId' => $authorId"?
Noah Yamen
CC BY-SA 3.0
2017-05-24T07:22:16.677Z
Just did a test of this and it works for me?  Are you testing with a username you know that actually exists?
Brad Bell
CC BY-SA 3.0
2017-05-24T15:42:29.097Z
Yes, currently since the site is only in development there are only 3 existing users. And I know for a fact that I'm using the exact username in every instance.
Noah Yamen
CC BY-SA 3.0
2017-05-24T15:58:05.887Z
The issue for me seems to be with the `'author' => $author` criteria. Because `craft()->users->getUserByUsernameOrEmail($username);` is definitely working, it's successfully recognizing an existing user. But the author criteria doesn't seem to work in conjunction with the EntryModel. Only authorId works for me here. I've also tried using `'author' => 'noahyamen'` (my username in craft) and it still doesn't filter the results. (And I know for a fact the entries have different authors other than me).
Noah Yamen
CC BY-SA 3.0
2017-09-17T21:06:37.997Z
Pretty sure you need to use `'authorId' => $author->id` in your criteria, not `'author' => $author`.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2017-05-19T22:47:51.900Z
2017-09-18T03:34:43.343Z
2017-09-18T03:34:43.343Z
Brad Bell
<p>Struggling to get this feed work with Element API. I'm trying to return all entries created by a specific username. I've tried several different methods, but none of them work. </p>

<p>Here is the latest version of what I've tried:</p>

<pre><code>'dealers/&lt;username:{username}&gt;.json' =&gt; function($username) {
  return [
    'elementType' =&gt; 'Entry',
    'criteria' =&gt; [
      'section' =&gt; 'listings',
      'username' =&gt; $username
    ],
    'paginate' =&gt; false,
    'transformer' =&gt; function(EntryModel $entry) {
      ...
</code></pre>

<entries><plugin-elementapi>
Element API: how can I return entries via username?
# Element API: how can I return entries via username?

(Asked by: username_0 on 2017-05-19)

Struggling to get this feed work with Element API. I'm trying to return all entries created by a specific username. I've tried several different methods, but none of them work. 
Here is the latest version of what I've tried:
'dealers/.json' => function($username) {
  return [
    'elementType' => 'Entry',
    'criteria' => [
      'section' => 'listings',
      'username' => $username
    ],
    'paginate' => false,
    'transformer' => function(EntryModel $entry) {
      ...

**Tags:** entries, plugin-elementapi

---

## 1 answers

### Answer by username_1 on 2017-05-19. Score: 1

Completely untested, but something like this should work:
'dealers/.json' => function($username) {
    $author = craft()->users->getUserByUsernameOrEmail($username);

    return [
        'elementType' => 'Entry',
        'criteria' => [
            'section' => 'listings',
            'authorId' => $author->id
        ],
        'paginate' => false,
        'transformer' => function(EntryModel $entry) {
            ...

> **Comments:**
> **username_0**: While it didn't give me an error, this did not work. It just lists all entries within the 'listings' section, not filtering by username.  
> **username_0**: Additional note: I do have a working feed that does filter listings via authorID. But that's by using the author ID in the feed URL. In this feed's case, it has to have the username of the user. Is there a way to get the ID of a user via their username, and then pass it to " 'authorId' => $authorId"?  
> **username_1**: Just did a test of this and it works for me?  Are you testing with a username you know that actually exists?  
> **username_0**: Yes, currently since the site is only in development there are only 3 existing users. And I know for a fact that I'm using the exact username in every instance.  
> **username_0**: The issue for me seems to be with the `'author' => $author` criteria. Because `craft()->users->getUserByUsernameOrEmail($username);` is definitely working, it's successfully recognizing an existing user. But the author criteria doesn't seem to work in conjunction with the EntryModel. Only authorId works for me here. I've also tried using `'author' => 'noahyamen'` (my username in craft) and it still doesn't filter the results. (And I know for a fact the entries have different authors other than me).  
> **username_2**: Pretty sure you need to use `'authorId' => $author->id` in your criteria, not `'author' => $author`.  

---

