Forum

Thread tagged as: Problem, Runway

2 way collection relationships

I've got 2 collections: Talks and Speakers. I've got perch:related tags on the Talks template like this:

<perch:related id="speaker" collection="Speakers" label="Speaker">
  <a href="/speakers/<perch:content id="slug" type="slug" />">Find out more about <perch:content id="name" /></a> ›
</perch:related>

and I've set the relevant speaker for each Talk.

Is there a way to automatically populate a list of talks on the speaker page? It feels a bit inefficient having to add the speaker to a talk then go to the speaker's profile and add the talk to the speaker. I'm guessing there's some way of doing it on the page template with php, but maybe there's a Perch way of doing it. Either way, I'm a bit stumped. Do you know how I might solve this?

Thanks :)

Martin Underhill

Martin Underhill 5 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, you can filter on the related fields. So if you had the speaker slug in a routing segment named 'speaker', you'd do this:

perch_collection('Talks', [
    'filter' => 'speaker.slug', 
    'match' => 'eq', 
    'value' => perch_get('speaker'),
]);

Thanks as always for the swift reply! I'll not get a chance to sit down with this until tomorrow. Thanks for the help – I'll let you know how I get on :)

Hi Drew,

I got it working! Thanks for the pointers. Here's what I used on my speaker.php page:

  perch_collection('Talks', [
    'template' => 'talk_link.html',
    'filter'   => 'speaker.slug',
    'match'    => 'eq',
    'value'    => perch_get('s'),
  ]);

the template looks like this:

<perch:before>
  <section>
    <h1>Talks</h1>
    <ul>
</perch:before>

      <li>
        <a href="/talks/<perch:content id="slug" type="slug" />">
          <perch:content id="title" type="text" />
        </a>
      </li>

<perch:after>
    </ul>
  </section>
</perch:after>

Cheers!

Martin.