Jump to content

has_child selector?


thetuningspoon
 Share

Recommended Posts

Just ran into a requirement on one of my sites which I don't think PW supports yet. I'm trying to match all pages that have a certain child, but the child is not an immediate child of the page. It is two levels down:

 

Page

-- Child 1

---- Sub-child 1

---- Sub-child 2    <  I only want to select the parent Page if this one exists

-- Child 2

 

This would be similar to the has_parent selector, but in the opposite direction. I searched the forums but didn't find any discussion of this feature/request. I seem to recall seeing it brought up at some point in the past, though.

Link to comment
Share on other sites

Just a little FYI, for those who might try this in PW 2.7, this...

12 hours ago, tpr said:

$pages->find("children.children.id=1028")

..only 'works' in PW 3.x.

 

@tpr, I tried the code but it's only returning one page, the immediate parent of 'Sub-child 2', i.e. Child 1? Is that how it works? 

@thetuningspoon

I'm not sure I follow your requirement. In the text you say:

12 hours ago, thetuningspoon said:

I'm trying to match all pages that have a certain child, but the child is not an immediate child of the page

You want all pages, meaning your want parent, grand-parents, etc, i.e. Child 1 and Page? However, in the illustration, you say: 

Quote

 I only want to select the parent Page if this one exists

Here it seems you only want Page but not Child 1?

If you want Child 1 and Page (and any other parent in between) you can also do:

$parents = $pages->get(1028)->parents;

I have a feeling you already know this, meaning I misunderstood you :).

Link to comment
Share on other sites

@kongondo Correct, I only want Page and not Child 1. Though it doesn't matter if Child 1 is matched too, because it has a different template from Page and I can exclude it by specifying the template in the selector.

@tpr I tried your suggestion yesterday after seeing your post. The example I gave in my initial post was oversimplified. My selector is actually more like children.children.pageField=123 

So that didn't work. Any ideas?

I restructured the page tree to get things working for now (the pages I'm referencing in the selector are now direct children), but this may not be ideal in the long run.

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