rash Posted August 18, 2020 Share Posted August 18, 2020 Hi, I would like to sort pages by the title of their Select Options field direction. Its easy to sort them by their option ID but not by their title. The ID’s/titles are: 1=north 2=west 3=south 4=east I would like them sorted alphabetically: east, north, south, west. What I’ve tried so far: $tracks = $pages->find("template=basic, sort=direction); // sorts north, west, south, east following the ID’s $tracks = $pages->find("template=basic, sort=direction->title); // sorts nothing at all $tracks = $pages->find("template=basic, sort=direction.title); // error: field not found If there’s no better way, I could change the select options so that ID’s and titles have the same order. Unfortunately there are quite a few pages that would need to get edited, so I’d like to avoid that. Thanks in advance Link to comment Share on other sites More sharing options...
louisstephens Posted August 18, 2020 Share Posted August 18, 2020 I believe the option field is set up as the following: id=name|Title. You are trying to access a Title that doesnt exist, so you would need to add to your existing set up like: 1=north|North 2=west|West 3=south|South 4=east|East I was mistaken after rereading the docs (sorry about that). It is set up by id=title by default, but you can add a separate title to the value (my apologies). 1 Link to comment Share on other sites More sharing options...
MoritzLost Posted August 18, 2020 Share Posted August 18, 2020 Have your tried sorting by the value instead? See Separate Option Values. If you have defined only titles so far, you can just add the values without having to change existing pages (since the values are stored by ID). Not entirely sure, but sorting by value might work (sort=direction.value). In any case, I have also found that the Select Options fieldtype is kind of lacking in selector support, and searching / sorting by values and titles does not always work. I now prefer custom page templates with page reference fields in most situations for this reason, they are way easier to use in selectors. 1 Link to comment Share on other sites More sharing options...
rash Posted August 18, 2020 Author Share Posted August 18, 2020 @louisstephens @MoritzLost Thanks to both of you. I forgot to mention that I tried it with separated values before, unfortunately the same thing happened. In any case the db table field-direction just stores IDs so sort=direction sorts by those numbers. sort=direction.value is leading to the same error as sort_direction.title (1054 Unknown column) and sort=direction->value as well as sort=direction->title do no sort at all. I was hoping it would be just a syntax problem, obviously things are a bit more complicated … Link to comment Share on other sites More sharing options...
Robin S Posted August 18, 2020 Share Posted August 18, 2020 There's an open issue in the old PW repo about the inability to sort by the "sort" subfield of a Select Options field: https://github.com/ryancramerdesign/ProcessWire/issues/2049 Looks like the "title" and "value" subfields don't work either. I've opened a new issue here: https://github.com/processwire/processwire-issues/issues/1231 @rash, in the meantime, if sorting by title is critical to your site perhaps the best thing would be to replace your Select Options field with a Page Reference field. When you've created the Page Reference field and added it to the relevant templates you could execute some API code in the Tracy Debugger console to transfer the Select Options field values to the Page Reference field (match option title to page title). 1 Link to comment Share on other sites More sharing options...
rash Posted August 19, 2020 Author Share Posted August 19, 2020 @Robin S Thank you for the information and opening a new issue. The sort option on the mentioned site is not critical, but as I work with Select Options quite often, I’m looking forward to a fix in the hopefully not too distant future. I agree to your (and @MoritzLosts) suggested Page Reference workaround – it’s one of my favourite field types, but sometimes seems a bit oversized for minor settings. Link to comment Share on other sites More sharing options...
MoritzLost Posted August 19, 2020 Share Posted August 19, 2020 @rash One possible workaround to this problem is to use a hidden text field that acts sort of like an index to your Selectable Option fields. I've used this approach before to make the selected options searchable (since their titles can't be searched, as you noticed). This would also work for your sorting problem: Create a text field (let's call it direction_index) and add it to the templates where you have your direction field. Use the Pages::saved hook to automatically write the current title (or value) of the direction field to the direction_index field. Now you can use the hidden direction_index field for sorting and searching. Since it's a regular text field, it has it's own database table and has full support for all ProcessWire selectors. Though of course that's probably more work than just migrating the the select field to a page reference ? 1 Link to comment Share on other sites More sharing options...
rash Posted August 20, 2020 Author Share Posted August 20, 2020 @MoritzLost This is a very elegant approach indeed! I’ve already built it in and it works flawlessly. I also appreciate it’s temporary nature that allows me to keep everything like it is, and just get rid of the helper field and the hook as soon as the select issue might be fixed someday. So another big thanks for that! (Btw: time comsumption was manageable, 5 minutes or so …) 1 Link to comment Share on other sites More sharing options...
rash Posted November 4, 2020 Author Share Posted November 4, 2020 Update: The issue was obviously fixed by @ryan and is working fine now. Tested with PW 3.0.168. Thanks a lot! 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