Pluck assigned pages from A to B

My website contains pages for artists and pages for projects.
In the project page I can select children from the artists page, called artist.

  artist:
    type: pages
    query: site.children.template("artists").children

This is simple, because each project has at least 1 artist to select from.

Now what I want is to show the assigned projects in the artist page.
In the project page I just call $page->artist()->toPages();.

  <?php $artists = $page->artist()->toPages();
    foreach($artists as $artist): ?>
      <a href="<?= url($artist) ?>"><?= $artist->title() ?></a>
    <?php endforeach ?>

But how is it with the artist page?
I was thinking of filtering each project that has the name of the artist, but is there another, more efficient was of doing that?

No, that’s the way to go forward.

Hey first of all thank you :slight_smile:
I have another more advanced question.
I could just filter each page, but the pages field stores an array, because there can be more then one artist assigned to a project.
So I cant really use the filterBy('artists' $page->title(), ',')

Here is my suggested solution (We are in a artist page):

<?php
$title = $page->title(); #artist 01
$projects = $site->children()->template('projects')->children(); #project 01, project 02, etc.

foreach($projects as $page):

  foreach ($page->artist()->toPages() as $related):

    $artist_project = $related->title();

    if($artist_project == $title): #I thought to compare the current page title with the pages in project, that is probably not cool
    #What else can I do? I'm stuck.
    #After the if i want to echo the title and url of the correct project, where the artist is assigned to.

        echo $page->title();

    endif;

  endforeach;
  
endforeach ?>

It’s so simple in theory, but I just cant get over the conversion from the array to the correct name, and then compare each project->artist.

Quick question, what is stored in your pages field, usually that is not the title but the page id…

If I assign 2 artist in one project, it looks like that:

Artist:

- artists/artist-01
- artists/artist-02

This should work:

$projects = page('projects')->children()->listed();

$currentArtistProjects =  $projects->filter(function($project) use($page) {
  $artists = $project->artist()->toPages();
  if ($artists->has($page)) {
      return $project;
  }
});