cb2004 Posted November 8, 2016 Share Posted November 8, 2016 I have been doing battle for the last couple of hours on this, I think I have got it but I just want to double check. Also I couldnt find anything on the forums so this could serve as help for other users. I have a set of child pages that hold a date. I want to do a find on those children for those with date>=today and then spit out the parents to then handle further logic. I feel like I am missing something that ProcessWire can do here so please let me know if you know of a better way. $results = $page->find("date_1>=today, sort=date_1, parent.status<" . Page::statusUnpublished); foreach ($results as $p) $parents[] = $p->parentID; $results = new PageArray(); $results->add($parents); ProcessWire should handle the unique parents as the first $results may return 1001,1002,1001 for example. Thanks as always people. Link to comment Share on other sites More sharing options...
LostKobrakai Posted November 8, 2016 Share Posted November 8, 2016 PageArray's are automatically unique. $found = $page->find("date_1>=today, sort=date_1, parent.status<" . Page::statusUnpublished); $results = new PageArray(); foreach ($found as $p) $results->add($p->parent()); Link to comment Share on other sites More sharing options...
cb2004 Posted November 8, 2016 Author Share Posted November 8, 2016 Thanks @LostKobrakai, that works well. The array is being automatically sorted to how the admin page tree is setup (I think), is there anyway to ignore this and have the order as they were added to the array? Link to comment Share on other sites More sharing options...
BitPoet Posted November 8, 2016 Share Posted November 8, 2016 The find call returns the pages in the order in which they are configured to be sorted. A PageArray keeps the order in which the pages were added (minus duplicates, for which the last addition counts) unless you explicitly call its sort method. There is one exception to the rule, which is when you pass a PageArray to add() or append(). In that case, the items in that PageArray get prepended. I'm not sure if this is intentional. 1 Link to comment Share on other sites More sharing options...
horst Posted November 8, 2016 Share Posted November 8, 2016 (edited) Not sure, but I think it is intentional. But also, you always can (re)order / sort it after adding a complete PageArray at once: http://cheatsheet.processwire.com/pagearray-wirearray/sorting-and-filtering/a-sort-property/ EDIT: Ok, thats not my evening. @BitPoet has already linked to PageArray->sort(), also to the newer API docs. Good night all together. Edited November 8, 2016 by horst 2 Link to comment Share on other sites More sharing options...
cb2004 Posted November 9, 2016 Author Share Posted November 9, 2016 $results = $pages->find("parent=$page->children, date_1>=today, parent.status<" . Page::statusHidden); $parents = new PageArray(); foreach ($results as $p) $parents->append($p->parentID); Append will get around the sort issue, cheers all. Link to comment Share on other sites More sharing options...
horst Posted November 9, 2016 Share Posted November 9, 2016 Isn't it, that you have an PageArray already in $result? Why not sorting this instead of the loop and double PageArray? Or do I get it wrong, what you are after? Link to comment Share on other sites More sharing options...
Robin S Posted November 10, 2016 Share Posted November 10, 2016 12 hours ago, cb2004 said: $results = $pages->find("parent=$page->children, date_1>=today, parent.status<" . Page::statusHidden); $parents = new PageArray(); foreach ($results as $p) $parents->append($p->parentID); Append will get around the sort issue, cheers all. This is the same as what @LostKobrakai showed in post 2: append() is identical to add(), and adding a page ID to a PageArray achieves the same as adding a Page object. 12 hours ago, horst said: Isn't it, that you have an PageArray already in $result? Why not sorting this instead of the loop and double PageArray? Or do I get it wrong, what you are after? The objective is to get the parents of the items in the first PageArray. 1 Link to comment Share on other sites More sharing options...
cb2004 Posted November 10, 2016 Author Share Posted November 10, 2016 13 hours ago, Robin S said: This is the same as what @LostKobrakai showed in post 2: append() is identical to add(), and adding a page ID to a PageArray achieves the same as adding a Page object. The objective is to get the parents of the items in the first PageArray. append() kept the order that the items were added to the array, add() kept the order that is setup within admin. Link to comment Share on other sites More sharing options...
Robin S Posted November 10, 2016 Share Posted November 10, 2016 4 hours ago, cb2004 said: append() kept the order that the items were added to the array, add() kept the order that is setup within admin. They really are the same thing: Quote Append an item to the end of the WireArray This is a functionally identical alias of the WireArray::add() method here for naming consistency with the WireArray::prepend() method. https://processwire.com/api/ref/wire-array/append/ https://github.com/processwire/processwire/blob/36984e4a057268b7a45b848e1b3b6ee757583459/wire/core/WireArray.php#L956-L959 1 Link to comment Share on other sites More sharing options...
horst Posted November 11, 2016 Share Posted November 11, 2016 19 hours ago, Robin S said: They really are the same thing double-like! 1 Link to comment Share on other sites More sharing options...
cb2004 Posted November 13, 2016 Author Share Posted November 13, 2016 So yeah, turns out add and append were exactly the same, must have been having a tough week as I thought my testing skills were ok :). I had to go with php array_unique to achieve what I wanted. I will post a code snippet in the morning. Thanks all. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now