How to create "related pages" field, where related pages would be selected from other page children?

Hi, looking for solution:

I have page “Events” with children pages “Event”.
I have page “Sponsors” with children pages “Sponsor”.

I need to add field to “Event”, that would let select any “Sponsor”.

(Practical purpose is to have all sponsors in “Sponsors” page, and then to add sponsors of certain events as related, so that client doesn’t have to create them from scratch every time, because every event has repeated sponsors, but not all).

By an example I see that it should be something like this in blueprint of “Event”, but how to connect to another page?

  # Related
  related:
    type: pages
    max: 10
    parent: query
    query: page.parent.children.listed.flip
  # Related
  related:
    type: pages
    max: 10
    query: site.find('sponsors').children.listed

Assuming that your sponsors page is also called sponsors (as its slug) and a top level page.

3 Likes

Great, works perfect. (Yes, slug is “sponsors” too).

Can you also help with display? What code should be in event.php template?

<?php

	foreach($site->find('sponsors')->children()->limit(10)->filterBy(_____?_______) as $sponsor):
	
?>

Actually found out already :slight_smile:
This works:

<?php foreach($page->related()->toPages()->limit(10) as $sponsor): ?>

$relatedPages = $page->related()->toPages();
foreach($relatedPages as $p):

// do something

endforeach;

Should do the job.

1 Like

Thank you, works too.

Yep, same code, your were faster.

As per documentation, the pages field doesn’t have a parent property.
(To prevent confusion: The field lets you select from the pages that the query returned, and not children of those)