Weighted sort

21 January 2013, 16:15

Keywords: link, sort

Sometimes you need to sort articles in a non-trivial sort order. Suppose that some of your articles are “important”, so you want them to stay on the top of your site. But you don’t want them to be sticky: when a new article is published, even an unimportant one, it should stay at the top for some moment, and then descend among its pairs.

There are many possible solutions. One of them consists to introduce some weight custom field (say, custom_2), and assign important weights to “important” articles. Now you can sort them as needed with, say

<txp:article sort="TIMESTAMPDIFF(HOUR,Posted,NOW())/(custom_2+1)" />

This works well, and other constructions are possible. For example, the articles on this site are dynamically sorted according to the comments_count value. Post few comments to see how it works. :)

The only glitch is <txp:link_to_prev/next /> not respecting this complex sort attribute, falling back to Posted desc, at least in the version 4.5.4. Awaiting a core solution, you can use etc_link_to plugin, that will do just fine.

File(s)