jacmaes Posted February 11, 2019 Share Posted February 11, 2019 I'm working on a photographer's site. My tree is organized as such: Home --Photography ----Abstractions ------Photo 1 (photo on its own page; template: portfolio_image) ------Photo 2 ----Animals ----Signs ----etc. --Places: ----United States (template: country) ------New Jersey (template: state) --------Newark (template:city) --------Ringwood --------etc. ------New York In each photo's page (e.g. "Photo 1"), I have a page reference field name "city" where I can select the city (e.g. "Newark"). Now in "Places", I'm trying to retrieve all the photos in each country, for example all pics taken in the United States. I need to find the grandparent of "city", i.e. the country where that city belongs to, but "city.has_parent=$country" or "city.parent.parent=$country" don't work: <?php $foreach ($page->children as $country): $photos_by_country = $pages->find("template=portfolio_image, city.has_parent=$country"); foreach ($photos_by_country as $photos): echo $photos->title; endforeach; endforeach; ?> What's the selector for "the grandparent of the city field" is…? Thanks for your help. Link to comment Share on other sites More sharing options...
bernhard Posted February 11, 2019 Share Posted February 11, 2019 Dont know if that's the best option, but you could use two selectors: $cities = $pages->findIDs("template=city, parent=yourcity"); $photos = $pages->find([ 'template' => 'photo', 'city' => $cities, ]); Another option would be a saveReady hook that populates the country on each photo save. And I'm quite sure there are even better solutions ? 2 Link to comment Share on other sites More sharing options...
jacmaes Posted February 11, 2019 Author Share Posted February 11, 2019 Thanks a lot @bernhard. You put me on the right track. I just changed "parent=yourcity" to "has_parent=$country". For posterity, here are the two selectors in my case: $cities = $pages->findIDs("template=city, has_parent=$country"); $photos = $pages->find([ 'template' => 'portfolio_image', 'city' => $cities, ]); 1 Link to comment Share on other sites More sharing options...
bernhard Posted February 11, 2019 Share Posted February 11, 2019 You could also use sub-selectors: https://processwire.com/docs/selectors/#sub-selectors Link to comment Share on other sites More sharing options...
jacmaes Posted February 11, 2019 Author Share Posted February 11, 2019 That's why I tried first, but has_parent does not seem to work as a sub-selector. Link to comment Share on other sites More sharing options...
bernhard Posted February 11, 2019 Share Posted February 11, 2019 $photos = $pages->find("template=portfolio_image, city=[template=city, has_parent=$country]"); This does not work? Link to comment Share on other sites More sharing options...
jacmaes Posted February 11, 2019 Author Share Posted February 11, 2019 I've tried and double-checked, but no, for some reason, "has_parent" does not seem to work in this case, even if I manually replace $country with a valid parent id. "Parent" does work as expected, but it's of no use here. A bug, or a limitation with "has_parent" maybe? 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now