Multiselect value as filter

Hi there!

I would like to show all the artworks related to an artist on the artist page. My current solution works when there is only one artist by artwork, and fails when there are multiple artists.

The content is structured like this:

  • Artists
    • Artist 1
    • Artist 2
  • Artworks
    • Artwork 1
    • Artwork 2

I am using the Artists subpages to populate a multiselect field in Artworks subpages:

artists:
  type: multiselect
  label: Artist(s)
  options: query
  query:
    fetch: site.index.template("artist").limit(100)
    value: "{{ page.title }}"
    text: "{{ page.title }}"
    translate: false

My code in the artist.php template:

<?php $artist = $page->title()?>
<?php foreach (page('artworks')->children()->listed()->filterBy('artists', $artist) as $artwork): ?>
   ...
<?php endforeach ?>

My understanding is I should split the artist field values, but don’t know how to make it happen…

$artist = $page->title();
$artworks = page('artworks')->children()->listed()->filter(function($artwork) use($artist) {
  return in_array($artist, $artwork->artists()->split(','));
});

If I had to do it, I’d store the id instead of the title…but the code above should work for your use case.

2 Likes

Thanks much, it worked!