Jump to content
antoine.s

Sorting on multiple date fields

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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

@antoine.s 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.

Share this post


Link to post
Share on other sites

 

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

Share this post


Link to post
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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By snck
      Hi there,
      I have a problem constructing a selector that finds all pages that refer to pages with a specific template.
      I have pages using an event template and I want to show events based on a specific context. In this example I want to filter the results and only show event pages that relate to a specific template (exhibitions) in their page field related_pages.
      What I tried:
      $events = $pages->find("template=event, related_pages.template.name=exhibition"); Unfortunately it does not work (0 results).
      Same with this:
      $events = $pages->find("template=event, related_pages=[template.name=exhibition]"); At the moment I am helping myself with the following lines, but I have a strong feeling that there is a more efficient solution:
      $events = $pages->find("template=event"); foreach($events as $event){ if(!count($event->related_pages->find("template=exhibition"))){ $events->remove($event); } }  
      I really hope that one of you can help me out.
      Thanks in advance!
      Flo
    • By Kiwi Chris
      The selector in the following code included in a template is returning nothing, however if I take out the compId.resultsdate<={$today} bit, it works fine, although obviously not filtered on the date field.
      $today = strtotime(date('Y-m-d')); $setImages = $pages->find("template=competitionImage, compId={$page->id}, compId.resultsdate<={$today}, compSubject.name=s, imageRating.title=Merit|Honours,check_access=0"); Here's the results of an example from Tracey Debugger
      templates_id=79, resultsdate<=1587729600, status<2048   SELECT pages.id,pages.parent_id,pages.templates_id FROM `pages` JOIN field_resultsdate AS field_resultsdate ON field_resultsdate.pages_id=pages.id AND (((field_resultsdate.data<='2020-04-25 00:00:00' ) )) WHERE (pages.templates_id=79) AND (pages.status<2048) GROUP BY pages.id Over in my ready.php I have inside a hook that refers directly to the page template that's used for the pages in the page field above:
      $today = strtotime(date('Y-m-d')) $event->return = $event->pages->find("template=competition,eventEnd>={$today},eventStart<={$today}"); In this case the filtering on date fields (albeit different ones) works fine. Can anyone suggest why the filter on the date subfield of the page field isn't working?
      Just to confirm, I do have a date value in the field, and it is a date before today. 🙂
      The problem may be something blatantly obvious, but I can't for the life of me figure out why the selector is returning no results when I include the date filter.
    • By opalepatrick
      Hi, trying to order pages by modified date, date. I can do 'sort=-date_modified, sort=-date' but that will always put the modified date first regardless of actual date order. I just want to coalesce the two fields and sort. Any ideas would be appreciated.
    • By Roberts R
      I have simple page structure:
      category subcategory simple-product simple-product simple-product simple-product subcategory .... category2 so Im at category page and running
      $pages->find("template=simple-product, has_parent=$page, limit=8"); and it returns 0 pages
       
      but this returns 4 pages
      $pages->find("template=simple-product, has_parent=$page"); Can someone explain why limit does not work here?
      EDIT:
      So I did some tests and it seems that any limit=n where n is >= actual page count that is possible ... selector return 0 results.
    • By jds43
      Hello, I have a Page Reference by template radio button field to promote a certain page (only two options), but I'd like to target the page that isn't selected. This would be used dynamically throughout the site.
      Does anyone know how I could accomplish this? Would I use something like remove() or not()?
       
×
×
  • Create New...