Jump to content
jacmaes

Retrieving grandparent

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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 🙂 

  • Like 2

Share this post


Link to post
Share on other sites

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,
    ]);

 

  • Like 1

Share this post


Link to post
Share on other sites

That's why I tried first, but has_parent does not seem to work as a sub-selector.

Share this post


Link to post
Share on other sites
$photos = $pages->find("template=portfolio_image, city=[template=city, has_parent=$country]");

This does not work?

Share this post


Link to post
Share on other sites

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?

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...