Jump to content

Sorting on multiple date fields


antoine.s
 Share

Recommended Posts

Hi,

I'm sorry if this has been adressed before but I couldn't find anything about it.

I'm pretty new to processwire but not to php or object developpment.

I'm working on a blog and the request was to add a custom date field on articles that allows the user to override the "date" associated to the article. It's basically a publish date.

If set, that date is used to sort the article and also if the date is in the future the article will not be displayed on the website.

Here is the selector I used to sort my articles list before:

 $pages->find('template=news, sort=-created, limit=4');

and here is how I do it now :

 $pages->find('template=news,!news_date>today, sort=-created|-news_date, limit=4');

The only issue is that the sort does not sort a big mashup of all the dates regardless of whether it's in "created" or "news_date" but it sorts one and then the other.

So what I get is a list of dates like this

02 jul 2015 (news_date)

15 Jul 2014 (news_date)

2 Dec 2014 (created)

1 Nov 2014 (created)

The news_date list is on top of the created list.

Do you know how I could achieve this?

Thanks a lot

Link to comment
Share on other sites

Try reversing it. You want to sort by created only if news-date doesn't exist, so it should be news-date|created. I'm also not sure if you can reverse the sorting by adding a minus behind each property or if it would work instead simply with: sort=-news_date|created. If it doesn't work like that you can always leave the sort without the minus and use ->reverse() to reverse the order of the pageArray instead.

Link to comment
Share on other sites

I did try switching but then only the latest created where on top. I guess the latest "news_date" where at the bottom of the list.

and the minus signs -news_date|-created seem to work as each field is sorted from newest to oldest, it's just that it generates 2 sorts and it does not mix it all up unfortunately. But thanks for the tip.

Link to comment
Share on other sites

  • 1 year later...

 

On 11/3/2016 at 6:57 PM, melody said:

Did you ever find a solution to this issue? I'm dealing with the same kind of problem now - I want to sort by two dates, but at the same time, not one then the other.

Something like this? :

Of course, the selectors and the fields must be adjusted, but as far as I understand your issue, this one should also work when dealing with dates. 

BTW, welcome to the forum :) 

Edited by szabesz
typo
  • Like 4
Link to comment
Share on other sites

On 11/3/2016 at 2:44 PM, szabesz said:

 

Something like this? :

Of course, the selectors and the fields must be adjusted, but as far as I understand your issue, this one should also work when dealing with dates. 

BWT, welcome to the forum :) 

 

Thank you for the reply and the welcome!! I'm glad to be here.

This solution works great - thank you so much for pointing me in the right direction. :)

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...