Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-12-21 07:21:13

tye
Member
From: Pottsville, NSW
Registered: 2005-07-06
Posts: 859
Website

Sortt by custom field which is date picker field

I am using an txp:article tag and am trying to sort the results by custom field 1, which is the date picker used in the superb plugin glz_custom_fields.

The date is formatted as dd/mm/yyyy – and when I sort, the output is not as I expected… it doesn’t read a date, just numbers – so the sort is incorrect.

I am trying to create a listing of gigs, where the date & time of a gig is displayed on the page. I would like these articles to expire on the same date too.

So I am thinking that maybe I should be using something with the <txp:article time=“future” /> and the Posted/Expired fields… but I can’t see to get that to work (never used them before) – would I have to set a future datein the posted and expiry date in the expiry?

or maybe some smd if goodness

Offline

#2 2010-12-21 07:49:51

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: Sortt by custom field which is date picker field

You should be able to use time="future" and set a future date as your posted date. Dates that have passed will automatically no longer show. You don’t have to have them explicitly expire, they can simply no longer show. If you want a gigs archive you can then also use time="past".

You can also use the custom_field approach you are using together with msv_if_custom_article_date. That plugin currently uses a date format similar to yours but for better sorting I slightly modified that plugin so it used the date format YYYY-MM-DD, which will sort properly using the normal sort="your_custom_field asc". Should you have events that run over several days you can set up start_date and end_date custom fields and have the article only disappear once the end_date has elapsed.


TXP Builders – finely-crafted code, design and txp

Offline

#3 2010-12-21 08:07:32

tye
Member
From: Pottsville, NSW
Registered: 2005-07-06
Posts: 859
Website

Re: Sortt by custom field which is date picker field

Perfect Jacob – Thank you :)

I had looked at that plugin earlier today, but for some reason was using it on a page and not in the form :)

A second look and it all made sense

Thanks again

Offline

#4 2010-12-21 23:07:39

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

Re: Sortt by custom field which is date picker field

Thanks for posting the link to the plugin. I have struggled with this – maybe I’m pedantic, but I really don’t like the idea of using the posted date field to record a date associated with the article’s content.

By the way, what do you mean by “better sorting” when you change the date format?

Offline

#5 2010-12-22 00:52:02

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: Sortt by custom field which is date picker field

aslsw66 wrote:

what do you mean by “better sorting” when you change the date format?

jakob refers to the YYYY-MM-DD format, the only one which properly sorts dates in custom fields.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#6 2010-12-22 05:42:58

tye
Member
From: Pottsville, NSW
Registered: 2005-07-06
Posts: 859
Website

Re: Sortt by custom field which is date picker field

Did you do that by changing these lines to this?

$intDate1Jul = gregoriantojd($intFirstYear, $intFirstMonth, $intFirstDay);
$intDate2Jul = gregoriantojd($intSecondYear, $intSecondMonth, $intSecondDay);

Offline

#7 2010-12-22 09:16:53

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: Sortt by custom field which is date picker field

If I recall correctly I changed the section where the first date string is split into its components.

Original (DD.MM.YYYY):

$intFirstDay = $arrFirstDate[0];
$intFirstMonth = $arrFirstDate[1];
$intFirstYear = $arrFirstDate[2];

Modded version for YYYY-MM-DD:

$intFirstDay = $arrFirstDate[2];
$intFirstMonth = $arrFirstDate[1];
$intFirstYear = $arrFirstDate[0];

The separator you use is up to you and you can set it using dateseparator="-". The default is . (a dot). Note if input a specific date to compare against you still need to input the second date in the dd-mm-yyyy / dd.mm.yyyy format (depending on the date separator you use).

Last edited by jakob (2011-01-02 21:19:58)


TXP Builders – finely-crafted code, design and txp

Offline

#8 2010-12-22 22:21:35

tye
Member
From: Pottsville, NSW
Registered: 2005-07-06
Posts: 859
Website

Re: Sortt by custom field which is date picker field

Thanks jakob – I’ll test that out :)

Offline

#9 2011-02-02 03:53:53

tye
Member
From: Pottsville, NSW
Registered: 2005-07-06
Posts: 859
Website

Re: Sortt by custom field which is date picker field

OK following on from this… I am calling my form like this:

<txp:article_custom section=“gigs” limit=“20” form=“home_gigs” wraptag=“div” class=“home_gigwrap_inner” sort=“custom_1 asc” />

and my form is wrapped with this:

<txp:msv_if_custom_article_date is=“greaterequal” customfield=“1” dateseparator=”-”>// Content</txp:msv_if_custom_article_date>

What is happening is that the limit=“20” from the article_custom tag is including, but not displaying old articles – so it is only showing 10 at the moment (10 old dates, 10 new dates)

Is that how this should work?

Offline

#10 2011-02-02 14:34:54

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: Sortt by custom field which is date picker field

That is a problem with that approach, yes. Essentially the date filter happens after the 20 articles have been retrieved from the database, so if 5 are “struck off” by the date filter, you have 15 showing.

Possible options:
  • retrieve a really long number of articles (e.g. 999) and have no pagination. You might end up with a long page if you have lots of future dates.
  • take another approach and create your own date sql query (I guess with smd_query)
  • take a two step approach with the help of soo_article_filter. I can’t think of how off-hand but the principle is to make a larger search, then search for a subset of that.

For simplicity’s sake I went with the first of those as the number of future articles was never likely to be massive.


TXP Builders – finely-crafted code, design and txp

Offline

#11 2011-02-03 00:33:06

tye
Member
From: Pottsville, NSW
Registered: 2005-07-06
Posts: 859
Website

Re: Sortt by custom field which is date picker field

Thanks jakob – I can’t do a long list, as there are about 50 entries (tried that)… I’ll investigate these plugins :)

Offline

Board footer

Powered by FluxBB