Jump to content

->find() and sort=sort with nested articles


toni
 Share

Recommended Posts

Hi!

I've a nested page tree looking like:

1389758590_Bildschirmfoto2020-10-27um11_37_56.png.609add09f0161524e40b071e4ee97d83.png

 

I know like to pull articles of "aktuell = $page" by there sort order:

$articles = $pages->find("has_parent=$page, template=event, limit=50, sort=sort, hide_in_programm=0")

The strange thing is the page over13 – which – reflections on … which is sorted over this house is not a… home appears sorted under the later in $articles. In other words parent articles are not sorted over their children (while sorting of their childs is correct) and parent articles are in different order as in backend.

My guess is I do experience a result what the docs warn for:

But note that if the results have multiple parents, the resulting order isn't likely to be that useful.

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

I think one way to work around this is to check each child of the ultimate parent "aktuell" for children and if they exist continue the loop. 
As this would require heavy changes to my templates my question is:

 Is there another way to pull out all parent and children of "aktuell" in correct order and one shot?

 

Thanks for your help,

Toni

 

Link to comment
Share on other sites

https://processwire.com/api/ref/wire-array/import/

Quote

The strange thing is the page over13 – which – reflections on … which is sorted over this house is not a… home appears sorted under the later in $articles. In other words parent articles are not sorted over their children (while sorting of their childs is correct) and parent articles are in different order as in backend.

My guess is I do experience a result what the docs warn for:

That's correct, the sort order of a page's children is relative to that level of hierarchy, not global. So by using pages->find with the sorting order you get weird results. I don't think there is a way to get a hierarchically ordered result in one query.

Do you want your page array to be ordered depth-first (first page -> children of first page -> second page -> children of second page -> ...) or depth-first (first page -> second page -> ... -> children of first page -> children of second page -> ...)?

3 hours ago, toni said:

I think one way to work around this is to check each child of the ultimate parent "aktuell" for children and if they exist continue the loop. 
As this would require heavy changes to my templates my question is:

 Is there another way to pull out all parent and children of "aktuell" in correct order and one shot?

I assume you just need your $articles variable to be a flat array of all pages in their page tree order, correct? I don't think it can be done in one query, but you can use WireArray::add and WireArray::import to create your flat array of pages.

For depth-first order:

$articles = new \ProcessWire\PageArray();
foreach ($page->children('template=event, hide_in_programm=0') as $article) {
    $articles->add($article);
    $children = $article->children('template=event, hide_in_programm=0');
    if ($children->count()) {
        $articles->import($children);
    }
}

For breadth-first order:

$articles = new \ProcessWire\PageArray();
$parents = $page->children('template=event, hide_in_programm=0');
$articles->import($parents);
foreach ($parents as $article) {
    $children = $article->children('template=event, hide_in_programm=0');
    if ($children->count()) {
        $articles->import($children);
    }
}

 

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