Jump to content

Sorting pages by two fields, sort only by 2nd field, when 1st field is empty


intrigus
 Share

Recommended Posts

Hello, I'd like to sort pages by two selectors.

I have a custom field "startdesprojekts" it is of the type date.

I want to sort the pages by "startdesprojekts". Since it's possible that there is no date defined I'd like to sort by creation time, if "startdesprojekts" is undefined.

My code looks like this, but it doesn't do what I want.

$items = $page->children("include=hidden, sort=-modified")->append($pages->find("inNewsAnzeigen=1"))->filter("sort=-startdesprojekts, sort=-created");
Link to comment
Share on other sites

The sort selector does only fallback to the second parameter for items, where the first sort parameter is the same value. It's not combining both field values to sort afterwards – they might even be not comparable because of different types. 

  • Like 1
Link to comment
Share on other sites

In my case, both fields are of the same type, both are dates.

Are you sure that there is no possibility to sort by field1 if it exists and sort by field2 if field1 doesn't exist?

So I'd like to first sort by field1 and only sort by field2 if fields1 doesn't exist.

Link to comment
Share on other sites

Two page arrays result in this:

date 1 | date 2

-    1

-    2

-    3

1

2

3

4

Or the other way around. It's not merging the dates. But if all pages are fetched no matter what (no pagination) it works like that:

foreach($myPages as $p){
  $p->set('sortDate', $p->date1 ?: $p->date2); // Runtime field
}
$myPages->sort("sortDate");
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

you would have 2 options i think (besides the runtime sort field as described above), one is to sort the Pagearray after , using ->sort(field1|field2) or you could store the sortdate as a hidden field on the page using a module to write that field everytime the page is saved

  • 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...