Jump to content

using $pages->find with page reference field


torf
 Share

Recommended Posts

I'd need some advice how to search for specific pages that have use a certain value in a page reference field. The task is quite simple: find all pages with a certain template that also use a page reference field with a specific title. 

$pageList = $pages->find("template=myTemplate, {PRField->title}={$input->urlSegment1}");

I also tried to split it but to no avail:

$pageList = $pages->find("template=myTemplate, {PRField->title}={$input->urlSegment1}");
$finalpageList = $pages->find("{$pageList->PRField->title}={$input->urlSegment1}");

I'm absolutely positive that I'm missing a crucial point here.

Link to comment
Share on other sites

Make sure: in myTemplate settings>URLs you Allow URL Segments

then

// decomposed example
$title = $input->urlSegment1; 

// single
$ref = $pages->get("template=myTemplate, title=$title"); // get retrieve a single result
$pageList = $pages->find("template=myTemplate, prfield=$ref");

// or if many is needed
$refs = $pages->find("template=myTemplate, title=$title|Foo|Bar"); // find retrieve a page array
$pageList = $pages->find("template=myTemplate, prfield=$refs");

 

  • Like 2
Link to comment
Share on other sites

Thanks a lot. My mistake was to look for the title. That does not work directly. So with your help I managed to get the data I needed.

$searchterm = $input->urlSegment1; 
$myPage = $pages->findOne("template=Template1, title={$searchterm}");
$myList = $pages->find("template=Template2, PRfield={$myPage}");

That works as expected.

  • Like 2
Link to comment
Share on other sites

for interest I asked chatgpt if this can be done in a single selector. here is the answer:
 

Quote

 

Yes, you can combine these two queries into a single $pages->find() operation in ProcessWire. You can use a sub-selector to achieve this. Here's how you can do it:

$searchterm = $input->urlSegment1;
$myList = $pages->find("template=Template2, PRfield=(template=Template1, title={$searchterm})");

This code will first find the page with template=Template1 and title={$searchterm}, then it will find all pages with template=Template2 that have a reference to the found page in the PRfield.

 

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...