Jump to content

Get Great Grandparent from field value


hezmann
 Share

Recommended Posts

Hi All,

Just recently started with ProcessWire and coming along ok but am struggling with figuring out how to get pages based on values of fields in children of children.

What I have is a structure like this...

  • Walks
    • Walk Variations
      • Walk Segments 

Walk Segments has two page reference fields (to Walk Towns) Start Town and End Town.   Walk Towns has a Maps Marker field which I've added the field Continent to.

What I'm trying to get is a list of all walks that are in a particular continent.  

Any help would be most appreciated!

Heather

 

Link to comment
Share on other sites

Hello.

Dont know if I understand you well, but maybe this:

// get childrens what are in desired continent
$europe_walks = $pages->find('template=walk-segments, start_town.continent.title="Europe"');
// or can use selector with continent page id eg. "start_town.continent=10050"

$variations = array(); // can use this inside foreach to store page arrays id

foreach($europe_walks as $variation){
    echo $variation->title . "<br>"; //Walk Segments
    echo $variation->parent->title . "<br>";//Walk Variation
    echo $variation->parent->parent->title . "<br>";//Walks			
}

Second option can be this:

//directly get walk variations
$europe_walks = $pages->find('template=walk-variations, children.start_town.continent.title="Europe"');

Regards.

 

Link to comment
Share on other sites

1 hour ago, hezmann said:

Walk Towns has a Maps Marker field which I've added the field Continent to.

Not quite sure what you mean here. You somehow edited the Map Marker fieldtype to include a continent subfield? Or do you just mean you added a field Continent to the Walk Towns template? It might depend a bit on what type of field Continent is, but something like this should work:

$walks_in_africa = $pages->find("template=walk, (children.children.start_town.continent=Africa), (children.children.end_town.continent=Africa)");

 

  • Like 1
Link to comment
Share on other sites

I actually added some extra fields (state, region, country, continent) to the module so the geocode grabs those for me from Google (well, technically I'm looking up the continent from a table with all countries/continents because Google doesn't provide that) so it would be refered to as (assuming the Maps Marker field is named marker) marker.continent.

I'll give the suggestions a go - Thanks!

  • Like 1
Link to comment
Share on other sites

13 minutes ago, hezmann said:

I actually added some extra fields (state, region, country, continent) to the module so the geocode grabs those for me from Google

Sounds very useful. Please share your module fork when you're ready. :-)

Link to comment
Share on other sites

Sorry about that - hit enter and it submitted then couldn't edit.  What I meant to say was...

 

Will try to remember to do that.  Need to clean the code up some first. :blush:

Thanks @Olsa and @Robin S.  I was able to get what I needed with...

$walk_segments = $pages->find('template=walk_segments, start_town.marker.continent.title="Europe"');
foreach($walks_segments a as $thewalk){
     echo $thewalk->parent->parent->title;
}

I just need to catch duplicates now but that should be easy enough!

Link to comment
Share on other sites

5 minutes ago, hezmann said:

I was able to get what I needed with...

$walk_segments = $pages->find('template=walk_segments, start_town.marker.continent.title="Europe"');
foreach($walks_segments a as $thewalk){
     echo $thewalk->parent->parent->title;
}

I just need to catch duplicates now but that should be easy enough!

Glad you have it working. Just a couple of things to be aware of if you do it this way:

1. Depending on how many Walk Variations are within a Walk, and how many Walk Segments are within a Walk Variation, you could end up loading way more pages into memory than you actually need (you only need the Walk pages). But if the number of pages isn't large then this could be fine.

2. If there are a lot of Walks in the continent, and for efficiency you want to paginate them into groups of 20 for example, this becomes difficult. Because you don't know how many Walks will result from any set of Walk Segments until you loop over them.

  • Like 1
Link to comment
Share on other sites

@hezmann

40 minutes ago, hezmann said:

Thanks @Olsa and @Robin S.  I was able to get what I needed with...

$walk_segments = $pages->find('template=walk_segments, start_town.marker.continent.title="Europe"');
foreach($walks_segments a as $thewalk){
     echo $thewalk->parent->parent->title;
}

If this works for you, than I mean that also you can try this example what is in basic the same what Robin S write in first answer:

//please note template in selector, in this case go from top to bottom
$walk_segments = $pages->find('template=walks, children.children.start_town.marker.continent.title="Europe"');
foreach($walks_segments a as $thewalk){
	echo $thewalk->title;
}

 

 

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