HOWTO: add a custom sort order to the topic lists

HOWTO: add a custom sort order to the topic lists
0

This is how to add a custom sort order to the topic lists.

class ::TopicQuery
    SORTABLE_MAPPING["order_name"] = "order_by"
end 

Replace order_by with the name of a column of topics table by with you want to sort.

You can also order by custom fields by replacing order_by with custom_fields.your_topic_custom_field_name to sort by the said custom field.

To apply sorting according to the said order, just add a GET param to the url ?order=order_name where order name is the key of the SORTABLE_MAPPING hash where you put your column name.

Example:
Recently, I got a requirement to sort topics by the time they were posted in the last time, here’s what I did.

class ::TopicQuery
    SORTABLE_MAPPING["latest_post"] = "last_posted_at"
 end 

To apply this sort, do https://youdomain.com/latest?order=latest_post

3 Likes