Hot answers tagged

17 votes
Accepted

How do I sort entries by two variables?

If you want to order by multiple columns that include custom fields, first make sure you're at least on Craft 2.4.2684, then you'll be able to use this syntax: {% for gallery in craft.entries.section(...
user avatar
  • 66.5k
14 votes
Accepted

How can I order entries by title when each title is numeric?

You can do that using MySQL’s cast() function: craft.entries.section('mySection').order('cast(title as unsigned)') Alternatively, you might get better performance if you create an actual Number ...
user avatar
13 votes

Sort merged array by array value in Twig?

The SuperSort Plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value. For ...
user avatar
  • 3,400
12 votes
Accepted

In Craft 2.5, how can you sort the entry index page by a custom field?

Currently, the only way to add custom fields to the sortable attributes is to write a custom plugin which makes use of the modifyEntrySortableAttributes hook, e.g.: public function ...
user avatar
9 votes
Accepted

How do you sort elements queried from multiple channels?

The order param sets the ORDER BY part of the MySQL query. You can also pass MySQL functions in it. In your case GREATEST() should do the job: {% set allEntries = craft.entries .section(['...
user avatar
  • 8,326
8 votes
Accepted

Sort merged array by array value in Twig?

This is only possible with a custom Twig Extension, since sorting a multidimensional array can't be done with Twig itself. I've written a plugin which extends Twigs sorting methods, which you could ...
user avatar
  • 8,326
7 votes
Accepted

Sort entries by most popular

Depends on how you want to determine "most popular"... If you want to calculate based on page views, then check out Ben Croker's Entry Count plugin (as Brad mentioned in his answer). If you want ...
user avatar
  • 23.6k
7 votes
Accepted

Sorting entries A-Z, ignoring 'The' and grouping by first letter

That SQL code snippet Brad posted only works with the order parameter. The group filter doesn't allow SQL, but you could try to replicate the logic using Twig's ternary operator. {% set artists = ...
user avatar
  • 35.9k
6 votes
Accepted

Want to sort a table field from Twig

You can't do that with core Craft functionality or Twig filters, but as Mats already suggested in the comments, you can do it with a plugin like "Super Sort". The template code to sort the array ...
user avatar
  • 35.9k
5 votes
Accepted

Display additional fields (and sort by) in Entries in Control Panel

Not at the moment. Sorting by custom field in the back end entries screen is not yet implemented although I know it is definitely possible in the future based on the way the code is currently laid out....
user avatar
  • 6,682
5 votes

How can I use Garnish to sort?

Sure, I've had experience with two different types of sorting with SuperTable (check the link for associated JS). If you're modelling the fieldtype after a table, you might like to use the Craft....
user avatar
  • 3,402
4 votes

How can I order entries by title when each title is numeric?

Twig doesn't have a natsort() filter. But I've put together a simple plugin which extends the sort-filter: https://github.com/victor-in/Craft-TwigBetterSort
user avatar
  • 8,326
4 votes
Accepted

Sort matrix blocks in admin by a field within the matrix?

There is nothing that will automatically sort blocks for you in the Control Panel. Without writing a plugin with some very fancy JavaScript, the only way to arrange blocks is manually. You can ...
user avatar
  • 23.6k
4 votes
Accepted

Custom order related entries

The natural sort order for related entries is the order defined in the control panel (not the post date of the related entries), unless you otherwise specify the sort order. Here is an example taken ...
user avatar
4 votes
Accepted

Sorting matrixblocks by earliest date from child table

This is a bit tricky to do from the template, but what I would suggest is to query for the Matrix blocks once, than loop through the blocks and store the data to a new custom array. The idea is, to ...
user avatar
  • 35.9k
4 votes
Accepted

Sort entries by global variable category

One solution is to retrieve the entry ids for the season entries and all entries separately; subtract the 'season entry ids' from 'all entry ids' (using craft's without twig filter); then merge the ...
user avatar
4 votes
Accepted

Sort / Order Entries on select or button click

Okay so you can either do this through ajax or through a page reload, a page reload is going to be much easier, but you can get it to work without reloading the page... Reloading the page You can ...
user avatar
  • 4,519
4 votes

Group and Sort Alphabetically

Seems I needed to put in the order filter {% set categories = craft.categories.group('programs').order('title') %} {% set categoriesGroup = categories|group('title|slice(0, 1)|replace("/[^a-zA-Z]/",...
user avatar
  • 373
3 votes
Accepted

How do I insert a special entry into a sections list?

There's a special loop variable available in for loops which provides info about the iteration. In your case you'd just insert that list item after the 2nd iteration: {% for entry in entriesOnPage %} ...
user avatar
  • 35.9k
3 votes
Accepted

Using `getPrev` and `getNext` with entries sorted by SuperSort plugin?

It won't work with getPrev and getNext, as they only work with ElementCriteriaModels and not with an array of Entry models. But that is what you get returned from the plugin. Supersort converts your ...
user avatar
  • 35.9k
3 votes
Accepted

How to order matrix blocks in reverse

I went and looked at the MatrixBlockModel; there's an attribute named sortOrder (If you output {{sortOrder}} inside the for loop, it will display the index of that block). You can use that attribute ...
user avatar
  • 3,205
3 votes
Accepted

How can I join plugin data with entry data based on uri?

You could of course manually create a select query with a join that fetches entries ordered by hits descending, however you will basically be recreating what $criteria->find() does above. I imagine ...
user avatar
  • 6,341
3 votes

Modifying search scoring algorithm

Seems to be the new standard since version 2.2.2582: "Entry and category search results now place a 5X weight on search term matches within Title fields."
user avatar
  • 1,009
3 votes

Sorting an array of entries according to a fixed list/array of items

Edit: This strategy still works, but see my updated answer for more up-to-date solution. Prior to SuperSort 3.1.0, you might achieve this by using the replace filter inside SuperSort's micro-template: ...
user avatar
  • 3,400
3 votes
Accepted

Using Supersort with date field

Thanks for asking about this, Cole! There was a bug that was causing rsortAs to return erroneous results. The issue is fixed in the 1.0 release. Grab yourself a fresh copy and you should be squared ...
user avatar
  • 3,400
3 votes
Accepted

How do I sort a collection of entries from multiple sections by date?

Rather than fetching the entries at first, just grab their IDs using ids() instead of find(): {% set featuredBlogId = craft.entries.section('blogs').limit(1).ids() %} {% set featuredArticleId = craft....
user avatar
3 votes
Accepted

If merging two sections from different locales how do I then sort them by a shared criteria?

With that last {% set news = craft.entries.id(ids) %} criteria model you're querying in one locale only and there's no way to change that, Craft doesn't have a feature to set up a criteria model to ...
user avatar
  • 35.9k
3 votes
Accepted

Sorting across blocks (to get prev/next links)

You can get all of the works entries related to your contents Matrix field like so: {% set works = craft.entries({ relatedTo: { sourceElement: issue, field: 'contents.works' } }) %} They will ...
user avatar
3 votes
Accepted

Make sort dropdown keep property

In short, you need to set the selected attribute on the option element based on the sort order. So, if the sort order is title asc, then that option would have the selected (selected="selected") ...
user avatar
  • 1,623
3 votes
Accepted

Sort entries in a category via entry field

You are getting this error because the "entries" fields are always considered to be a query (ElementCriteriaModel) that can return more than one element. Therefore, if you know for sure that you will ...
user avatar
  • 496

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