Categories:
Author:
About It:
Rate it:
Flag it:
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!
Keywords/tags:
Posted here:
15 Jan 2006Article modified:
15 Nov 2006









Comments/questions? Leave a message in the form.
Commented (7)
sortby and sortdir have been merged into sort in 4.0.4.
— Textbook
Thanks! This was very helpful.
Do you know how to make prev and next links for these custom lists?
Thank you!
Thanks for the good tip.
It would be great if it could be updated to the new sort=”“ attribute.
Does anyone know a way to limit the selected articles by dates so that only articles with future dates for the next 3 months is viwed?
Subscribe to this article's comments RSS feed. [ ? ] View Recent Comments across the site. Get a Gravatar.
Would you care to write a comment?