Advanced Sort By
When trying to sort an article_custom tag, I was frustrated by the unpredictable behavior when using both a sortby=“a,b” and a sortdir=“desc”.
As it turns out, the “sortby” attribute is a direct interface to the database language SQL. That means you have all the sort power in the world.
If you know PHP/SQL, the field just works into the query “ORDER BY “ . sortby . “ “ . sortdir (publish.php line 622).
Enough about how it works, here’s how to hack it:
- If you want to sort by comments and title, with the most comments first and title from A-Z:
- sortby=“comments_count DESC, Title ASC” sortdir=”“
- If you want to sort by the first category from A-Z, and the title from A-Z:
- sortby=“Category1 ASC, Title ASC” sortdir=”“
- If you want to sort by custom fields 3 from Z to A, 4 from A to Z, and 7 from Z to A:
- sortby=“custom_3 DESC, custom_4 ASC, custom_7 DESC” sortdir=”“
For my movie reviews blog, I keep the movie’s release year in the custom_3 field — so to sort by titles grouped in 2005, 2004, 2003…, I used sortby=“custom_3 DESC, Title ASC” sortdir=”“.
Hope this helps!