Hot answers tagged

9 votes
Accepted

Order by score and another field

I assume you're using .search() – a common mistake is to try to combine orderBy('score') with custom fields, which doesn't work because the score attribute isn't an actual field, but a computed value ...
user avatar
5 votes
Accepted

Craft Commerce: Sort Products By Price

Close! This should do it: {% set products = craft.commerce.products({ relatedTo: (category), order: 'defaultPrice', }) %} With the quotes it's a string. Without the quotes, Twig is ...
user avatar
  • 66.4k
4 votes
Accepted

Method (plugin free?) to combine search results across multiple searches (and element types)

To get this working I submitted a patch to 'SortByField' - https://github.com/dannynimmo/craftcms-sortbyfield - to get this plugin to work with Commerce products - and I now do this: {% set ...
user avatar
4 votes
Accepted

Output entries sorted by category title and paginate

Not exactly what you're looking for, but it gets you closer. This should display the order you want, but it doesn't have any pagination (and honestly I'm not sure if that's possible using Craft's {% ...
user avatar
  • 66.4k
3 votes
Accepted

Query two sections filtered by different custom fields, then order them

There's this super useful SuperSort plugin that would make this possible. {% set sortAsTemplate = '{{ object.section == "Exhibitions" ? object.startDate|date("U") : object.date|date("U") }}' %} {% ...
user avatar
  • 35.8k
2 votes
Accepted

Multisort with supersort plugin

Try to group the entries sorted by size using the group filter: {% set entriesSorted = entries|supersort('sortAs', '{ size }', SORT_NUMERIC) %} {% set entriesByBrand = entriesSorted|group('brand....
user avatar
  • 35.8k
2 votes
Accepted

Sort entries by custom date in Matrix field / Super Table

I did something like this as a workaround: Loop over entries, fetching all data you need and adding it to an array. Sorting that array with SuperSort and than displaying all info by looping over the ...
user avatar
  • 1,456
2 votes
Accepted

Retrieve entries by random sort, then resort alphabetically

I recon Supersort is they way to go, as its got some really handy functions to make this sort of thing a breeze. Try with the following: {% set entries = craft.entries.section('presenters').order('...
user avatar
  • 3,392
2 votes

Nesting level too deep - recursive dependency?

I figured this out, with help from a GitHub thread or too. It appeared that using the sort filter on an array of objects was problematic. I was able to fix this by sorting the keys in speakers instead ...
user avatar
  • 736
2 votes
Accepted

Sort Repeater fields alphabetically

You can use the orderBy parameter to get super table blocks in the specified order. For example, if your super table field is called countries and the title field (that you store the country's name in)...
user avatar
  • 6,315
2 votes
Accepted

sort by checkbox field with multiple options and by postDate desc

The equivalent SQL would be something like SELECT * FROM players ORDER BY teamName IS NOT NULL DESC, postDate DESC; but I'm not sure how that would translate in a simple element query. It'd be ...
user avatar
  • 571
2 votes
Accepted

How to create ordered list of categories from multiple category groups

You can query on multiple category groups like so: craft.categories.group('suitability, finish, shape, material, edge, surface').all() It can also be done with an array: craft.categories.group([ '...
user avatar
2 votes

Syntax for Multisort Filter

For those of you reading through this post, I thought it might be useful to provide a bit of background on my question. The Craft Docs list "multisort" under Filters and it sounded like something that ...
user avatar
  • 1,311
1 vote

Syntax for Multisort Filter

I think this should do what you're looking for: {% for rides in allRides|multisort(['eventDate'],['SORT_DESC'],['SORT_REGULAR']) %}
user avatar
  • 66.4k
1 vote

sort by checkbox field with multiple options and by postDate desc

I believe this won't work the way you want it with a single query, especially with reliable performance. The problem is that you actually want 2 different sort criteria being combined. I would ...
user avatar
1 vote
Accepted

Order matrix blocks by date field

I hope I am not completely missing the point, but I guess like that: Initial 5: {% for block in block.eventList.eventDate('>=' ~ (now|atom)).orderBy('eventDate asc').limit(5).all() %} {{ ...
user avatar
1 vote

Order grouped results by dropdown order

You could wrap this in another for loop and put a condition on the inner fors, something like {% for option in myPositionDropdown %} <h2>{{ option.value }}</h2> {% for position, ...
user avatar
1 vote
Accepted

Help with correct syntax regarding multisort please

Try removing the quotes on 2nd/3rd arguments: {% set rides = furniture|multisort(furniture['price'],[SORT_DESC],[SORT_REGULAR]) %}
user avatar
  • 2,437
1 vote

How to reorder structure entries in the Admin panel?

I don't know if there's a quick/easy way to do this within the admin area, but if you go to Entries, then click on your Structure, there's a little 4-dot symbol to the left of the Entry's title (make ...
user avatar
1 vote

Sorting by date field across sections not working

Your second attempt is sound. I don’t really have an idea why it doesn’t work, but I bet it has to do with the SQL that is caused by setting the section param to an array. What I would try next is ...
user avatar
  • 35.8k
1 vote

Sort entries by custom date in Matrix field / Super Table

I used the preparse plugin to save a date value from a SuperTable field into a static field as 'yyyymmdd'. I could then sort my entries based on that static field.
user avatar
1 vote
Accepted

Extend order methods to sort numbers naturally

It's possible to pass MySQL functions to the order parameter. Using the MySQL LENGTH function, I believe the following should do the trick (assuming your "model number" field's handle is modelNumber): ...
user avatar
1 vote

Method (plugin free?) to combine search results across multiple searches (and element types)

You could do all of this in PHP. Build your criteria models, merge the results and sort them with usort.
user avatar
  • 35.8k

Only top scored, non community-wiki answers of a minimum length are eligible