2

I need to order the results of a search query after I got back the results. The reason to do this is that I want to make different lists on the same page (with the same results) and I dont't want to search over and over again just to reorder te results. Can I do that? So basically I do this:

{% set results =  craft.entries.section('player').search('team:"thenameoftheteam"') %}

What I want to do with this result is: loop

  • through the results and order them by playerPosition
  • show the longest player (lengths are in the field playerHeight)
  • show the oldest player (birth dates are in the field playerBirth)

I could run this query 3 times and order by a different field each time, but I don't think that's the way to go.

3

Not really feasible without a plugin – fortunately, there's the excellent SuperSort:

{% set results =  craft.entries.section('player').search('team:"thenameoftheteam"') %}

{% set resultsByPlayerPosition = entries|supersort('sortAs', '{playerPosition}') %}
{% set resultsByPlayerHeight = entries|supersort('sortAs', '{playerHeight}') %}
{% set resultsByPlayerBirth = entries|supersort('sortAs', '{playerBirth}') %}

If you need the sorted results in the opposite order, you can just apply the |reverse filter, i.e.

{% set resultsByPlayerHeight = entries|supersort('sortAs', '{playerHeight}')|reverse %}

If e.g. playerPosition is a relational field (a category, perhaps), you'll need to do something like this:

{% set resultsByPlayerPosition = entries|supersort('sortAs', '{{ playerPosition.first.title }}') %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.