Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2005-11-10 06:56:31
- capa
- Member
- From: Montana, USA
- Registered: 2005-09-10
- Posts: 20
sorting article lists
How do I make Textpattern display a list of news stories sorted first by date, and then by a category called “important”?
eg: (list of stories)
(published 11/09/05 at 5 p.m.) Mayor Re-Elected by 500 Votes <—-(this is marked important)
(published 11/09/05 at 7 p.m.) Car Dealer Celebrates 10th Anniversary <—-(not marked important, but published more recently)
(published 11/08/05) Asteroid Demolishes Couple’s Home <——(marked important, but published one day earlier)
“I like the look of my own text.”
Offline
#2 2005-11-10 07:10:26
- Mary
- Sock Enthusiast
- Registered: 2004-06-27
- Posts: 6,236
Re: sorting article lists
sortby="Posted desc, field(Category1, 'important'), field(Category2, 'important')"
That should work. I use a variation of this myself, so I know that it is possible. I’ve been meaning to mention this technique in the wiki and the tag usage forum, but I haven’t had the time.
Offline
#3 2005-11-10 20:55:51
- zem
- Developer Emeritus
- From: Melbourne, Australia
- Registered: 2004-04-08
- Posts: 2,579
Re: sorting article lists
I don’t think that’ll work, because it’ll sort by date and time first, and only consider the categories when the timestamp is identical.
Something like this might:
sortby="extract(year from Posted), extract(month from posted), extract(day from posted), field(Category1, 'important'), field(Category2, 'important')"
Alex
Offline
#4 2005-11-12 02:17:45
- Mary
- Sock Enthusiast
- Registered: 2004-06-27
- Posts: 6,236
Re: sorting article lists
I discovered field(Category2, 'important')"
doesn’t work, because sortby gets doSlash()-ed.
It worked for me with:
<txp:article form="static" sortby="field(id, 127,126)" sortdir="ASC" />
because there are no slashes
Offline
#5 2005-11-14 02:49:56
- capa
- Member
- From: Montana, USA
- Registered: 2005-09-10
- Posts: 20
Re: sorting article lists
Mary,
What is the significance of id, 127 and 126? Are those numerical codes for txp commands?
Last edited by capa (2005-11-14 03:20:43)
“I like the look of my own text.”
Offline
#6 2005-11-14 03:53:27
- capa
- Member
- From: Montana, USA
- Registered: 2005-09-10
- Posts: 20
Re: sorting article lists
OK, I got it working with
<code><txp:article sortby=“extract(day from Posted) DESC, Category1” /></code>
( this is still in the testing phase, so I simplified it for the sake of my own sanity )
Behold: http://tucker.ees.com/textpattern-sortby.jpg
The first story was posted the same day as the second story, but it was posted AFTER the second story. BUT the first story is in the “important” category. So far, so good. Thanks for your help.
Last edited by capa (2005-11-14 03:54:23)
“I like the look of my own text.”
Offline
#7 2005-11-14 03:56:54
- Mary
- Sock Enthusiast
- Registered: 2004-06-27
- Posts: 6,236
Re: sorting article lists
Mine above was for custom sorting my static articles by id, without having to do multiple calls to the article tag.
Offline
#8 2005-11-14 04:10:55
- capa
- Member
- From: Montana, USA
- Registered: 2005-09-10
- Posts: 20
Re: sorting article lists
Ah … I don’t quite follow that. Using your method, how would you write <code><txp:article sortby=“extract(day from Posted) DESC, Category1” /></code>?
“I like the look of my own text.”
Offline
#9 2005-11-14 04:29:57
- capa
- Member
- From: Montana, USA
- Registered: 2005-09-10
- Posts: 20
Re: sorting article lists
Also, is it a hack to add ASC or DESC immediately after a sortby value ie sortby=“extract(day from Posted) DESC, Category1” ?
Using ASC and DESC in this way is not mentioned at http://textpattern.net/wiki/index.php?title=Txp:article_/
But I did come across this method in different post from Mary at
http://forum.textpattern.com/viewtopic.php?id=12419
“I like the look of my own text.”
Offline
#10 2005-11-14 06:18:39
- Mary
- Sock Enthusiast
- Registered: 2004-06-27
- Posts: 6,236
Re: sorting article lists
> “…how would you write…”
I wouldn’t, that was my point. I was showing how I used field() to specially order my articles, so I knew the general idea should have been possible, even if I didn’t know the specifics. If that makes any sense.
> “Also, is it a hack…”
That depends on what you mean by hack.
Normally, when you make an SQL query, you can supply it with a column or list of columns, and list asc(ending) or desc(ending). As a quick example, say we have a table named “fruit”, with two fields/columns “apple” and “orange”. So, I ask the database to list all the rows, and sort them please.
SELECT apple FROM fruit ORDER BY oranges DESC
The Textpattern tags split this up into two tags, calling one sortby, and the other sortdir, so the query works like:
SELECT apple FROM fruit ORDERBY sortby sortdir
So, it’s not a hack if we’re talking about databases, this is standard SQL. It could be considered a hack for Textpattern, simply because it may not be obvious which tags you can do this for or not, and it certainly isn’t very clean.
Offline
#11 2005-11-14 06:39:26
- capa
- Member
- From: Montana, USA
- Registered: 2005-09-10
- Posts: 20
Re: sorting article lists
Cool thanks Mary :)
“I like the look of my own text.”
Offline
#12 2006-03-24 01:19:24
- mattgilbert
- Member
- Registered: 2006-03-23
- Posts: 70
Re: sorting article lists
Could someone point me to the documentation for the “field()” function, if there is any? I can’t find anything about it, and when I try to use it it doesn’t behave how I would expect.
I’m trying show all the artices in one category with something like:
txp:article form=“project” sortby=“field(Category1, ‘Projects’)”
where “project” is a form I made to use on articles in the “Projects” category. It could up with nothing, while the same code with:
txp:article form=“project”
displays all the articles fine. Just not limited to the one category that I’m wanting to show.
Last edited by mattgilbert (2006-03-24 02:20:14)
Offline