Jump to content

Rock page builder & page references


Atlasfreeman
 Share

Recommended Posts

While using the page reference from vanilla processwire with Rock page builder, I have a hard time finding the right syntax.

$link2 = $pages->find("page_reference_01=$page, sort=parent.sort, sort=sort");
 

Normally, I would use a line like this. But this does not quite work, as the page references are on a page in a block(on another page), while using this, I sometimes could target $block instead of $page, but it does not work this time.

What are the right way to find a page reference within an other page within a block.

$link2 = $pages->blocks->find("page_reference_01=$page, sort=parent.sort, sort=sort");
 
Link to comment
Share on other sites

Hey @Atlasfreeman thx for your question.

As you already mentioned correctly if the page reference field is in a block, when using a selector with that page reference field, you will get the $block and not the $page where the $block lives on.

Let's say we have a block "Authors" and there we have a page reference field to select an author.

We can get all these blocks like this:

$blocks = $pages->find('my_author_field=123');

Then you can loop over these blocks and get the related $page:

echo "<p>Blog Posts by this Author:</p>";
foreach($blocks as $block) {
	$page = $block->getBlockPage();

	// maybe add some additional checks here, eg check if $page is published etc.

	echo "<div><a href='{$page->url}'>{$page->title}</a></div>";
}

That should be all you need.

If that is not performant enough because you have to query a lot of pages let me know and I'll show you a more advanced technique. Also it might be necessary to add something like "include=hidden" or "include=all" to the selector of the blocks, I'm not sure.

If you need any more help with that let me know. Otherwise please share the working example once you are done and mark the topic [solved] thx 🙂 

Link to comment
Share on other sites

Ok while reading this I think it's worth sharing the other technique as well, as this might be even easier...

The trick is to add a hidden field to your $page, like "post_author". That could be a page reference field.

Then you add a hook on "Pages::saved" and you populate that field based on the $block's selected author.

Then you can use a selector as usual:

$posts = $pages->find("template=blogpost, post_author=123");

This has the benefit that you can use efficient queries and sorting etc. and you can rely on PW's find() for only finding published pages and such.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...