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

#1

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
#2
  # 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.

2 Likes
#3

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

#4

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):
	
?>
#5

Actually found out already :slight_smile:
This works:

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

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

// do something

endforeach;

Should do the job.

1 Like
#7

Thank you, works too.

#8

Yep, same code, your were faster.

#9

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)