⭐️ If you like Statamic, give it a star on GitHub! ⭐️

Make `collection:previous` and `collection:next` sort by `title`

Hello,

I would like the order of collection:previous and collection:next to go by alphabetical order of a field (such as title), rather than by date or manual sort order. The https://statamic.dev/tags/collection-previous and https://statamic.dev/tags/collection-next pages suggest it's possible: "If you're on a single entry page and want to show [previous and next] entries in order (publish date, alphabetical, or manual)." But I can't get it to work. I get the familiar error:

Exception
collection:previous requires ordered or dated collection

Here's the collection's config.

title: 'Test People'
template: test_people/show
layout: layout
revisions: false
route: '/test-people/{{ slug }}'
sort_dir: asc
date_behavior:
  past: public
  future: private
structure:
  root: false
  max_depth: 1

(When editing a collection's config, under Ordering > Orderable the CP says, "Enable manual ordering via drag & drop," but it doesn't indicate anything about enabling alphabetical ordering. So I had initially tried it without Orderable turned on and without max_depth set, but got the same error.)

Here's the relevant template code. (I've omitted collection:next for brevity since it's virtually the same.):

{{ collection:previous in="test_people" as="posts" limit="1" sort="title" }}
  {{ if no_results }}
    <li>&nbsp;</li>
  {{ /if }}

  {{ posts }}
    <li class="post">
      <a href="{{ url }}">{{ title }}</a>
    </li>
  {{ /posts }}
{{ /collection:previous }}

(I also tried sort="title:asc".)

The prev/next docs pages say, "All collection tag parameters are available," and the page it links to says:

Sort entries by field name (or random). You may pipe-separate multiple fields for sub-sorting and specify sort direction of each field using a colon. For example, sort="title" or sort="date:asc|title:desc" to sort by date then by title. To sort manually, use sort="order". (Make sure to set max depth to 1 for your collection).

Incidentally, for my collection, sort="date:asc" also gives the error, and using sort="order" does not give the error (which is not a surprise), but as I noted at the beginning, neither is what I'm looking to achieve.

Does anyone know how to make them sort by title?

Thanks very much!

Bruce

p.s. Update: I should add for anyone looking to achieve ordering by ascending publish date that omitting sort="date:asc" works; it appears to be the default behavior. Be aware, though, that if you later edit an entry, its date changes, making it now the most recent one, and thus pushing it to the end of the collection:next line. At least in my tests.

>>>>>>> Unanswered <<<<<<<
0 Replies
1 Follower