Jump to content

[SOLVED] Sorting pages by Select Options title, not ID


rash
 Share

Recommended Posts

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

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).

  • Like 1
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

@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

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).

  • Like 1
Link to comment
Share on other sites

@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

@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:

  1. Create a text field (let's call it direction_index) and add it to the templates where you have your direction field.
  2. Use the Pages::saved hook to automatically write the current title (or value) of the direction field to the direction_index field.
  3. 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 ?

  • Like 1
Link to comment
Share on other sites

@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 …)

  • Like 1
Link to comment
Share on other sites

  • 2 months later...

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...