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 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()?
       
    • By Hubris
      Hi there!
      I'm using some page reference fields to create lists of tags, categories, years, etc.. I'm able to find the pages like so:
      $pages->find("template=project, {$filter}={$page->title}"); Which dynamically does something like: 
      $pages->find("template=project, tags=Experimental"); Only if the value (the page name, like "Experimental") starts with letters. If it starts with numbers, find returns nothing.
      Why is this and how can I fix it?
    • By Elchin
      Hi.
      I want select pages where now between date and end_date or now bigger than date and end_date is empty.
      I have five tried variants:
      $start = strtotime(date('Y-m-d') . " 00:00:00"); $results = $page->children("foo=(date<$start,date_end=''),bar=(date<$start,date_end>=$start),sort=-date,limit=12"); $start = strtotime(date('Y-m-d') . " 00:00:00"); $results = $page->children("date<$start,(date_end='',date_end>=$start),sort=-date,limit=12"); $start = strtotime(date('Y-m-d') . " 00:00:00"); $results = $page->children("date_end=''|date_end>=$start,date<$start,sort=-date,limit=12"); $start = strtotime(date('Y-m-d') . " 00:00:00"); $results = $page->children("!date_end|date_end>=$start,date<$start,sort=-date,limit=12"); $start = strtotime(date('Y-m-d') . " 00:00:00"); $results = $page->children("date_end>=$start|!date_end,date<$start,sort=-date,limit=12"); All this variants not worked for me and returned zero results.
×
×
  • Create New...