bernhard Posted August 28, 2014 Share Posted August 28, 2014 hi, i have a blog template with the field "date", where i can override the date of the posting. in my template i show the date like this: <?= $page->get('date|created'); ?> that works perfectly! is there any possibility for a selector like this without looping them through php (i would like to use the built in pagination module) sort=-date|created Link to comment Share on other sites More sharing options...
kongondo Posted August 28, 2014 Share Posted August 28, 2014 (edited) specify each sort separately, e.g. $pages->get('/mypage/')->children('sort=-date, sort=created'); That will first sort by 'date' then by 'created' It's all documented here: http://processwire.com/api/selectors/#sort Edited August 28, 2014 by kongondo 3 Link to comment Share on other sites More sharing options...
bernhard Posted September 2, 2014 Author Share Posted September 2, 2014 thank you kongondo, but that's not what i want to achieve. i have a setup like this: title created time test1 30.8. test2 3.9. test3 5.9. 2.9. test4 5.9. 1.9. i want to sort these "blog posts" either by the date created OR (if not empty) by the time field. so it should output this: test1 test4 test3 test2 but with sorting seperately i get the list sorted by created and then, if created is equal, by time: test1 test2 test4 test3 my solution is to set the "time" field required and autofill to today and sorting only by "time", but i wanted to know if this kind of sorting was possible somehow similar to getting field values with the OR selector ($page->get('this|that')) Link to comment Share on other sites More sharing options...
kongondo Posted September 2, 2014 Share Posted September 2, 2014 (edited) OK... Sounds like a job for the new group selectors [OR-groups] See here: https://processwire.com/talk/topic/3768-processwire-dev-branch/?p=64049 Another example here: https://processwire.com/talk/topic/7370-search-for-one-field-or-another-but-with-different-values/ This other related improvement might also be of interest https://processwire.com/talk/topic/3768-processwire-dev-branch/?p=58722 Edited September 2, 2014 by kongondo 3 Link to comment Share on other sites More sharing options...
webaff Posted July 16, 2017 Share Posted July 16, 2017 Hi @bernhard, A while ago, but have you ever found a solution to sort the way you posted above? (OR-groups are no solution here, I guess.) I want to sort after fileField.modified in an language-alternate fields setup: Seems like language-alternate fields inside selectors do not work. The sort is always by default language. Sorting the way you are asking in this thread could solve this problem. I could sort by both, fileField.modified OR (if not empty) by fileField_altLang.modified... I would appreciate your reply, thx. 1 Link to comment Share on other sites More sharing options...
bernhard Posted July 16, 2017 Author Share Posted July 16, 2017 Hi webaff, I would create a hook on saveready populating a (hidden) field with whatever value you need and use this field for sorting. This way you have any freedom that PHP and the PW API give you Hope that helps Link to comment Share on other sites More sharing options...
webaff Posted July 17, 2017 Share Posted July 17, 2017 Hi bernhard, geat idea! I can see how that would work in your setup. Unfortunately I can not apply this to my situation. I have 4 file fields for the 4 different languages, according to the concept of language-alternate fields. There is always a file for the default language (mandatory user input). But uploading files for those other 3 languages is optional. Now I want to sort by file.modified depending on the user's language. (If there is a file for the user's language, take its modified date for sorting. If not, take the default file's modified date.) Link to comment Share on other sites More sharing options...
bernhard Posted July 17, 2017 Author Share Posted July 17, 2017 If I understand your setup correctly you could do one field for each language then? Modified de ... Timestamp Modified en ... Timestamp You could populate all fields on save and then just sort on the appropriate field ('..., sort=modified_' . $lang) 2 Link to comment Share on other sites More sharing options...
webaff Posted July 18, 2017 Share Posted July 18, 2017 @bernhard, I'm not sure if I explained my setup clearly. I would be glad if you could jump over to this thread where I explained it in more detail: Thank you. Link to comment Share on other sites More sharing options...
bernhard Posted July 18, 2017 Author Share Posted July 18, 2017 I understood it like this, my workaround would work I'm quite sure. Though I would file a github issue as I think it should work as you expect... Link to comment Share on other sites More sharing options...
Robin S Posted July 18, 2017 Share Posted July 18, 2017 @webaff, just to spell out what @bernhard is suggesting... For each language-alternate field you have, you can add a datetime or text field to the template to store the modified date. Here is an example hook for a single language: $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template->name !== 'basic-page') return; // match whatever template you need // Output formatting is off in saveReady so File fields are WireArrays regardless of field settings if(count($page->file_german)) { // Use the modified timestamp of the German file field if a file exists $page->modified_german = $page->file_german->first->modified; } elseif(count($page->file)) { // Otherwise use the default file field if a file exists $page->modified_german = $page->file->first->modified; } }); Then you sort by "modified_german" in your selector. 3 Link to comment Share on other sites More sharing options...
webaff Posted July 18, 2017 Share Posted July 18, 2017 @bernhard and @Robin S, it's all working. Thank you so much! Robin S, further up berhard proposes to file an issue at github. Do you agree? 1 Link to comment Share on other sites More sharing options...
Robin S Posted July 18, 2017 Share Posted July 18, 2017 1 hour ago, webaff said: Robin S, further up berhard proposes to file an issue at github. Do you agree? I don't think there is any bug to report here - a $pages->find() selector must ultimately become an SQL query and the selector sort options become an ORDER BY clause. You cannot do something in an SQL query like "sort by some column but if that column is empty for a row then use some other column instead". But I guess you could make a request in processwire-requests in case Ryan can come up with some wizardry to allow OR sorting. 2 Link to comment Share on other sites More sharing options...
webaff Posted July 18, 2017 Share Posted July 18, 2017 @Robin S, I filed this request at GitHub. Maybe you want to comment there to make things more clear. Thanks again for your support. 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