Get first created admin user?

I try to get data from the first created admin user. This is what I ended up with:

$users = site()->users();
$users = $users->filterBy('role', 'admin');
$user = $users->sortBy('date', 'desc')->first();
print_r($user);

Everything works except for the sortBy. I get the result alphabetically instead of getting the first created user. I get the user aaa instead of jens.

I also tried modified and created with the same result.

The user object does not have a date field, unless you have added one?

But should use sortBy('modified') even if I don’t have a field like that or created? None of them work.

Another topic: sortBy('modified') actually sorts by 'created' dates

I’m quite sure I’ve sorted by the created datetime before, but not on users.

These are the user methods: https://getkirby.com/docs/cheatsheet#user

The page object has modified but there is no created method you could sort by (unless you have a field of that name).

The user object:

User Object
(
    [username] => me
    [email] => me@you.cc
    [role] => admin
    [language] => en
    [avatar] => 
    [gravatar] => https://gravatar.com/avatar/6852d2af95e22a5ccf82325eeceb51f2?d=mm&s=256
    [isCurrent] => 
)
```

The page object:

```
Page Object
(
    [title] => My Home
    [id] => home
    [uid] => home
    [slug] => home
    [parent] => 
    [uri] => home
    [url] => http://localhost/k256
    [contentUrl] => http://localhost/k256/content/home
    [tinyUrl] => http://localhost/k256/x/vl2sb4
    [root] => /Users/sonja/htdocs/k256/content/home
    [dirname] => home
    [diruri] => home
    [depth] => 1
    [num] => 
    [hash] => vl2sb4
    [modified] => 2017-10-28T08:11:43+00:00
    [template] => home
    [intendedTemplate] => home
    [isVisible] => 
    [isOpen] => 1
    [isActive] => 1
    [isHomePage] => 1
    [isErrorPage] => 
    [isCachable] => 1
    [isWritable] => 1
    [content] => Content Object
        (
            [root] => /Users/sonja/htdocs/k256/content/home/home.txt
            [fields] => Array
                (
                    [title] => My Home
                    [intro] => Yay! If you are seeing this, the installation of Kirby worked. :-)
                    [text] => ## Get started

- Go to the (link: panel text: Panel) to give Kirby's admin interface a try
- Check out the (link: http://getkirby.com/docs text: docs) and start building your own site
- Follow (twitter: @getkirby) on Twitter for updates
- Visit the (link: http://forum.getkirby.com text: forum) to connect with other Kirby users
- Sign up to (link: https://getkirby.com/#kosmos text: Kirby Kosmos), our monthly newsletter
- (link: http://getkirby.com/support text: Get in contact) if you need support.

**Have fun with Kirby!**

(notice: info)
Some text here
(…notice)
                    [category] => 1
                    [feature] => closeup.jpg
                    [employmentsection] => -
  employer: Hermann
  year: "1964"
  based: Munich
  position: Chef
  description: jfajf ajsdfi
  _fieldset: employment
                )

        )

    [headers] => 
    [children] => Children Object
        (
        )

    [siblings] => Children Object
        (
            [0] => projects
            [1] => about
            [2] => blog
            [3] => contact
            [4] => error
            [5] => stations
            [6] => test
        )

    [files] => Files Object
        (
            [0] => boilerplate.mp4
            [1] => closeup.jpg
        )

)
```

As you can see, the page object has a modified field, the user object doesn't.

Thanks! :slight_smile: I will solve this another way, probably by some fancy glob solution with kirby()->roots()->accounts(),

An alternative would be to add a field to the user when the user is created via a user.create hook. But why is that information important?

Here is a part of my now working class:

function username() {
    $users = site()->users();
    $users = $users->filterBy('role', 'admin')->toArray();
    
    $files = $this->getFiles();
    foreach($files as $file) {
        if(array_key_exists($file, $users)) {
            return $file;
        }
    }
}

function getFiles() {
    $glob = glob(kirby()->roots()->accounts() . DS . '*.php');
    usort($glob, function($a,$b){
        return filemtime($a) - filemtime($b);
    });
    foreach($glob as $item) {
        $parts[] = pathinfo($item)['filename'];
    }
    return $parts;
}

It works fine now. Maybe it’s possible to clean it up but for now it’s good.

The hook is a good alternative, but not in my case. I will keep my secret a while longer what it’s for, but I will probably tell you later today. :slight_smile: