Content last modified Monday 6 March 2017
hop to #bottom
About It:
Rate it:
  • Helpful?
  • 2 Yes
  • 0 No
Flag it:

If you'd like to provide updated information and do not have access to directly edit, please contact the site admin; thanks!

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!

Article Request Count:
Posted here:
15 Jan 2006
Article modified:
15 Nov 2006

Comments/questions? Leave a message in the form.

Commenting is closed for this article.

Commented (7)

thanks. this is great. is there anyway to extend this and make the sortby=”rand()” more effective? i’d like to grab a few articles randomly from the archives instead of just diplaying my list in a random order.
Dat Chu:
This is a good tips. Definitely useful for many cases.

sortby and sortdir have been merged into sort in 4.0.4.

sort: This attribute’s value will be used as a SQL order by clause and can thus contain every table row of the txp_link database table followed by SQL sort order indications asc and desc respectively. Valid values include “url”, “linkname desc”, “description asc”, “date asc”, “rand()” and others.



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.

Would you care to write a comment?

You know you want to visit the Archives.




There are also tag clouds, 'cause those are fun.
Published with Textpattern