Adam Kiss Posted August 8, 2011 Share Posted August 8, 2011 Using latest PW2.1 rc: After picking up this topic: http://processwire.com/talk/index.php/topic,375.0.html I tried to use following selector to pick up random 4 pages out last added 40: $rndm = $pages->find('template=xxx, limit=40, sort=created')->find('limit=4, sort=random'); System returns 4 pages allright, but they are always the same. Documentation seems to not cover sort=random at all, som I'm little confused here. Thanks Ryan Link to comment Share on other sites More sharing options...
jbroussia Posted August 9, 2011 Share Posted August 9, 2011 Cache ? Link to comment Share on other sites More sharing options...
Adam Kiss Posted August 9, 2011 Author Share Posted August 9, 2011 Cache ? Turned off. Or rather, not set yet, so default value (should be 0) Link to comment Share on other sites More sharing options...
Pete Posted August 9, 2011 Share Posted August 9, 2011 Looking at the function getRandom() this should work: $rndm = $pages->find('template=xxx, limit=40, sort=-created')->getRandom(4); 4 in this case being the number of random results you want to return, and note the minus before created else you'll be getting the first 40 created pages for the template instead of the last 40. The actual getRandom function is on line 368 of wire/core/Array.php EDIT: Just tested it on my install and it does work, pulling 4 random pages from the last 6 in my case (easier to debug as I can remember the last 6 I added ). The full code I used was this (my template was game in this case): $rndm = $pages->find('template=game, limit=6, sort=-created')->getRandom(4); foreach ($rndm as $pg) { echo $pg->title . "<br />"; } 1 Link to comment Share on other sites More sharing options...
Adam Kiss Posted August 9, 2011 Author Share Posted August 9, 2011 Thanks Pete Link to comment Share on other sites More sharing options...
ryan Posted August 9, 2011 Share Posted August 9, 2011 Adam, your original selector would have worked, but it's an oversight. Technical explanation: That the second find() is going through WireArray::find() rather than $pages->find(). The WireArray::find is all done in memory, whereas the $pages->find() actually translates to a database query. The two are supposed to function similarly from API, but the WireArray version is missing a few things present in the $pages version (undocumented things, see below). The "sort=random" is one of those things, but no longer… I just fixed that in the latest commit, so your original selector should now work in the latest P21. https://github.com/ryancramerdesign/P21/commit/f083f7d4d433d6c0919101f2c17b15059b6ab2b4 For reference, the other [undocumented] things not yet implemented in the WireArray version are: status=hidden status=unpublished status=locked status=max include=hidden include=all check_access=1 sort=a, sort=b, sort=c (sorting by multiple fields) These will all be implemented, and then documented once consistent across the $pages->find() and WireArray::find() versions. Beyond the above undocumented items, the two should otherwise function the same at present. Also note that Pete's version is a good way to go, and perhaps more efficient than using a second selector. 1 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