Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-12-14 23:25:06

ibob
Member
From: Finland
Registered: 2010-06-14
Posts: 35

Sorting articles with custom field

I want to sort my articles with custom field.. lets say custom_1. Field includes number or it’s empty like this:

Article_1 | custom_1 = “”
Article_2 | custom_1 = “3”
Article_3 | custom_1 = “1”
Article_4 | custom_1 = “2”

If I use basic article_custom sort=“custom_1 desc” articles will be sorted like this:

Article_2 | custom_1 = “3”
Article_4 | custom_1 = “2”
Article_3 | custom_1 = “1”
Article_1 | custom_1 = “”

And with asc custom_1 = “” will become before custom_1 = “1”.

What I want is list where value of custom field is articles position where value 1 is first, 2 second, 3 third etc. and articles that are empty will be last ones:

Article_3 | custom_1 = “1”
Article_4 | custom_1 = “2”
Article_2 | custom_1 = “3”
Article_1 | custom_1 = “”

I tried something like:

<txp:article_custom sort='<txp:if_custom_field name="x">custom_1 desc</txp:if_custom_field>, Posted desc'> <txp:title /> </txp:article_custom>

But it doesnt work since its not “inside” of the loop.

Any ideas what to do?

Offline

#2 2010-12-15 00:53:47

aslsw66
Member
From: Canberra, Australia
Registered: 2004-08-04
Posts: 342
Website

Re: Sorting articles with custom field

Not sure about your specific case, but custom fields are simply stored as text in the back end, which means any sorting is text based. I ran into this problem when trying to sort on a date in a custom field and discovered that “30” comes before “4”.

Offline

#3 2010-12-15 02:56:58

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: Sorting articles with custom field

You can do this by using two article lists:

<txp:article_custom my_field_name="_%" sort="custom_1 asc" />
<txp:article_custom my_field_name="" sort="Posted desc" />

This uses the technique discussed here. Some caveats also discussed in that thread. “my_field_name” is the name of the field corresponding to custom_1.


Code is topiary

Offline

#4 2010-12-15 06:53:02

ibob
Member
From: Finland
Registered: 2010-06-14
Posts: 35

Re: Sorting articles with custom field

aslsw66 I’ve been doing product catalogs with textpattern and in some cases I need to lift certain products before others. When I tried to find solutions I found from other topic that you can use custom field value as numeric value by adding (custom_x+0) <txp:article_custom sort="(custom_1+0) asc" /> forum.textpattern.com/232004

jsoo, it works! Thank you! That _% was totally new thing to me.. there’s actually lots of other places where I need it aswell.

Offline

#5 2010-12-16 03:14:23

aslsw66
Member
From: Canberra, Australia
Registered: 2004-08-04
Posts: 342
Website

Re: Sorting articles with custom field

Wow, never saw that. I assume that if I can store a date in a custom field as a number (ie. not as a text string), this should work properly.

Offline

Board footer

Powered by FluxBB