1

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/<username:{username}>.json' => function($username) {
  return [
    'elementType' => 'Entry',
    'criteria' => [
      'section' => 'listings',
      'username' => $username
    ],
    'paginate' => false,
    'transformer' => function(EntryModel $entry) {
      ...
1

Completely untested, but something like this should work:

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

    return [
        'elementType' => 'Entry',
        'criteria' => [
            'section' => 'listings',
            'authorId' => $author->id
        ],
        'paginate' => false,
        'transformer' => function(EntryModel $entry) {
            ...
6
  • 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
    May 22 '17 at 13:51
  • 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
    May 22 '17 at 14:59
  • Just did a test of this and it works for me? Are you testing with a username you know that actually exists?
    – Brad Bell
    May 24 '17 at 7:22
  • 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
    May 24 '17 at 15:42
  • 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
    May 24 '17 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.