Jump to content

easier way to grab the grandchildren of a page and exclude child of current page if it is iteself a parent ?


protro
 Share

Recommended Posts

Hello Processwire peoples. I am a new user and very much enjoying the possibilities afforded by this CMF.

I was trying to accomplish

$page->children()

but exclude returned pages that themselves contained children (I am calling them grandchildren here). I came up with this finally, which seems to work. It excludes the child page that contained children and merges its children with the rest of the original page's children, and sorts in descending chronological order.

I am using Rockfrontend and Latte:

{var $children = $page->children()->not("children.count>0")}
{var $grandChildren = $page->children("children.count>0")->children()}
{var $items = $children->append($grandChildren)->sort("-date")}

{foreach $items as $item}

I find the documentation here a bit confusing. For example I wish I had a list of the system fields belonging to a page. I have trouble locating what the available selectors are.

Any help or alternatives much appreciated ?

Link to comment
Share on other sites

Welcome @protro ?

What you have is fine.

This is another way you could do it, which is perhaps a bit more readable:

$items = new PageArray();
foreach($page->children as $child) {
	if($child->hasChildren) {
		$items->add($child->children);
	} else {
		$items->add($child);
	}
}
$items->sort('-date');

And if the objective happens to be "I want all the descendants that don't themselves have descendants" then this is potentially quite a bit more efficient because it's only a single DB query:

$items = $page->find("children.count=0, sort=-date");

 

  • Like 6
Link to comment
Share on other sites

4 hours ago, protro said:

I find the documentation here a bit confusing. For example I wish I had a list of the system fields belonging to a page. I have trouble locating what the available selectors are.

Have a look at the docs, here are two pages I find very helpful.

https://processwire.com/api/ref/page/

https://processwire.com/docs/selectors/

Cheer's

 

  • Like 2
Link to comment
Share on other sites

6 hours ago, Robin S said:

And if the objective happens to be "I want all the descendants that don't themselves have descendants" then this is potentially quite a bit more efficient because it's only a single DB query:

$items = $page->find("children.count=0, sort=-date");

You can also use the selector with the children() method:

$page->children("children.count=0, sort=-date");

 

  • Like 4
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...