# Get first created admin user?

**URL:** <https://forum.getkirby.com/t/get-first-created-admin-user/8885>\
**Category:** Questions\
**Tags:** v2\
**Created:** [October 30, 2017, 7:59am UTC](https://forum.getkirby.com/t/get-first-created-admin-user/8885 "2017-10-30T07:59:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [October 30, 2017, 7:59am UTC](https://forum.getkirby.com/t/get-first-created-admin-user/8885/1 "2017-10-30T07:59:31Z")

</div>

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

```php
$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.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [October 30, 2017, 9:02am UTC](https://forum.getkirby.com/t/get-first-created-admin-user/8885/2 "2017-10-30T09:02:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [October 30, 2017, 9:07am UTC](https://forum.getkirby.com/t/get-first-created-admin-user/8885/3 "2017-10-30T09:07:04Z")

</div>

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](https://forum.getkirby.com/t/sortby-modified-actually-sorts-by-created-dates/7155)

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [October 30, 2017, 9:08am UTC](https://forum.getkirby.com/t/get-first-created-admin-user/8885/4 "2017-10-30T09:08:37Z")

</div>

These are the user methods: [https://getkirby.com/docs/cheatsheet#user](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).

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [October 30, 2017, 9:14am UTC](https://forum.getkirby.com/t/get-first-created-admin-user/8885/5 "2017-10-30T09:14:03Z")

</div>

The user object:

````auto
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.
````

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [October 30, 2017, 9:15am UTC](https://forum.getkirby.com/t/get-first-created-admin-user/8885/6 "2017-10-30T09:15:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [October 30, 2017, 9:17am UTC](https://forum.getkirby.com/t/get-first-created-admin-user/8885/7 "2017-10-30T09:17:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [October 30, 2017, 9:32am UTC](https://forum.getkirby.com/t/get-first-created-admin-user/8885/8 "2017-10-30T09:32:18Z")

</div>

Here is a part of my now working class:

```php
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. 🙂

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:31pm UTC](https://forum.getkirby.com/t/get-first-created-admin-user/8885/9 "2025-02-06T17:31:51Z")

</div>

Archived on February 6, 2025, 5:31pm UTC


