Quick Tip: Sorting "Date" Type TV Values

By YJ Tso
September 10, 2015
Quick Tip: Sorting

Custom fields in MODX, known as "Template Variables" or "TVs" for short, can expose a multitude of input types for CMS content managers. One commonly required input type is the "Date" field.

However, depending on your use case, the fact that this input type saves its values in the database as a (standards compliant) string instead of a UNIX timestamp, can either save you time or cause all kinds of headaches.

One of the cases where you might prefer a timestamp is when you want to sort on the datetime values. Jason Coward's getResources Snippet has an option for sorting by datetime TV values, but if you are writing your own queries and would prefer the values to be stored as timestamps, you can do it with a smart little plugin written by MODX staffer John Peca.

Here's the gist of it. NOTE: I wrote the comments—John is not self-aggrandizing ;)

With timestamps, not only can you sort but you can perform comparisons, to return Resources with TV values within a range, for example. It's just another little tool we've found useful on a few occasions. If you have any tricks in your tool bag, please share it in the comments, or the MODX Forums. The MODX Community Slack account is another great place to share knowledge and ask questions.