Pages Fields Relationships

panel

#1

Hello, I am new to Kirby but I’m loving it so far!

I currently have two page types one called “Projects” and the other called “Artists”. In both pages, I have a pages field where I can select multiple pages of the other page type. How would I create a relationship so when I add a related “Project” page to the “Artist” page field, it also adds the current “Artist” page to that related “Project” page?

Thanks in advanced :slight_smile:


#2

The question is if it isn’t completely sufficient if this information is available in the project page.

If you then want to list the projects that belong to a given artist, you can filter the projects by artist.

This way you can still have your many-to-many relationship but without having to duplicate the relations.

If you still want to do this, you can use hooks.

When working with relationships, I’d recommend using the AutoID plugin to give each page a unique key that is then stored as reference (foreign key). This is necessary to make sure that the reference to the page is still valid in the future, even if the referenced page is later deleted.


#3

Thanks for getting me started! My only question is how do you use AutoID with the pages field? In the documentation, it shows how to use the AudioID with a structured field but I don’t see anything about how to use it with the pages field. Thanks again


#4

I’m afraid it won’t work with a pages field, because you need a query option that accepts an array, i.e. a select or multiselect field:


fields:
  related:
    type: multiselect
    label: Related pages
    options: query
    query:
      fetch: page("notes").children
      text: "{{ page.title }}"
      value: "{{ page.autoid }}"