Filter pages by current user in panel

Hi all,

I’ve been looking around for an answer to this and have found parts which give a hint as to where to go but no solid route.

I’m looking to filter all the pages on the site by user so that the current user sees all the pages which they’ve been added to via the users field present on each page.

I’ve seen that there is the pagesdisplay plugin which allows for sorting via pages fields but how, if it is possible, can you filter the pages by the current user? If this is not possible what would be the best way to achieve this?


Only subpages of a certain page or pages that could live anywhere in the structure? I assume this is about permissions to access pages, right?

Pages that could live anywhere in the site structure. No, I’m not as concerned about permissions in this instance as there will be a lot of crossover between authoring of pages. It would be nice have it so that only those added to the users section of a page would be able to edit it but not essential. Primarily I need to be able to show the current logged in user all the pages in which they have been added to as a user as there will be a large number of users and resulting pages and its to make it as easy as possible for them to find and edit the pages they are featured in.

In terms of site structure I’m aiming for as little nesting as possible with almost all content free floating in one folder which will then be accessed primarily through filtering rather than by parent children relationships. Does that complicate things?

That should be possible via the pagesdisplay plugin you mentioned above, because you can use any query and therefore filter by a field value. Since the user field uses yaml format for storing, you need a custom page method for this since you cannot use a callback in query language.

Just to make sure I’m not getting confused… What role would the custom page method be playing here? Would it just be turning the yaml format into something which can be filtered and returned? How would this be fed back into the pagesdisplay?.. Sorry for all the questions and thanks for your help so far!

Usually, you can for example make a query like this:

query: page.children.filterBy('category', 'somecategory')

However, to filter by users, you will need a callback like this in PHP (at least at the moment, in the future will will have a collection filter that will make this easier):

$filteredPages = $page->children()->filter(function($child) {
  return $child->userfield()->toUser() === kirby()->user();

You cannot do that in query language, therefore you would use a custom method in your query, e.g.

query: page.someMethod

Then in a plugin define this custom method:

Kirby::plugin('my/page-methods', [
    'pageMethods' => [
        'someMethod' => function () {
            return $this->children()->filter(function($child) {
               return $child->userfield()->toUser() === kirby()->user();
1 Like

Thanks so much! Definitely helped me understand the process of plugin + page method a lot better. I would have spent a long long while trying to get my head around it.

I added/switched in a bit from a section in the cookbook I came across as a result to accommodate for multiple authors with toUsers()->has()

Again thanks for the help!

A post was merged into an existing topic: Filter pages section by current user