Jump to content

selector: sort by 2 fields?


bernhard
 Share

Recommended Posts

hi,

i have a blog template with the field "date", where i can override the date of the posting.

in my template i show the date like this:

<?= $page->get('date|created'); ?>

that works perfectly! is there any possibility for a selector like this without looping them through php (i would like to use the built in pagination module)

sort=-date|created
Link to comment
Share on other sites

thank you kongondo, but that's not what i want to achieve.

i have a setup like this:

title		created		time
test1		30.8.		
test2		3.9.		
test3		5.9.		2.9.
test4           5.9.            1.9.

i want to sort these "blog posts" either by the date created OR (if not empty) by the time field. so it should output this:

test1

test4

test3

test2

but with sorting seperately i get the list sorted by created and then, if created is equal, by time:

test1

test2

test4

test3

my solution is to set the "time" field required and autofill to today and sorting only by "time", but i wanted to know if this kind of sorting was possible somehow similar to getting field values with the OR selector ($page->get('this|that')) :)

Link to comment
Share on other sites

OK...

Sounds like a job for the new group selectors [OR-groups] See here:

https://processwire.com/talk/topic/3768-processwire-dev-branch/?p=64049

Another example here:

https://processwire.com/talk/topic/7370-search-for-one-field-or-another-but-with-different-values/

This other related improvement might also be of interest

https://processwire.com/talk/topic/3768-processwire-dev-branch/?p=58722

Edited by kongondo
  • Like 3
Link to comment
Share on other sites

  • 2 years later...

Hi @bernhard,

A while ago, but have you ever found a solution to sort the way you posted above? (OR-groups are no solution here, I guess.)

I want to sort after fileField.modified in an language-alternate fields setup:

 

Seems like language-alternate fields inside selectors do not work. The sort is always by default language.

Sorting the way you are asking in this thread could solve this problem. I could sort by both, fileField.modified OR (if not empty) by fileField_altLang.modified...

I would appreciate your reply, thx.

  • Like 1
Link to comment
Share on other sites

Hi webaff,

I would create a hook on saveready populating a (hidden) field with whatever value you need and use this field for sorting.

This way you have any freedom that PHP and the PW API give you :)

Hope that helps

Link to comment
Share on other sites

Hi bernhard,

geat idea! I can see how that would work in your setup.

Unfortunately I can not apply this to my situation. I have 4 file fields for the 4 different languages, according to the concept of language-alternate fields. There is always a file for the default language (mandatory user input). But uploading files for those other 3 languages is optional. Now I want to sort by file.modified depending on the user's language. (If there is a file for the user's language, take its modified date for sorting. If not, take the default file's modified date.)

Link to comment
Share on other sites

If I understand your setup correctly you could do one field for each language then?

Modified de ... Timestamp

Modified en ... Timestamp

You could populate all fields on save and then just sort on the appropriate field ('..., sort=modified_' . $lang)

  • Like 2
Link to comment
Share on other sites

@webaff, just to spell out what @bernhard is suggesting...

For each language-alternate field you have, you can add a datetime or text field to the template to store the modified date. Here is an example hook for a single language:

$pages->addHookAfter('saveReady', function(HookEvent $event) {
    $page = $event->arguments(0);
    if($page->template->name !== 'basic-page') return; // match whatever template you need
    // Output formatting is off in saveReady so File fields are WireArrays regardless of field settings
    if(count($page->file_german)) {
        // Use the modified timestamp of the German file field if a file exists
        $page->modified_german = $page->file_german->first->modified;
    } elseif(count($page->file)) {
        // Otherwise use the default file field if a file exists
        $page->modified_german = $page->file->first->modified;
    }
});

Then you sort by "modified_german" in your selector.

  • Like 3
Link to comment
Share on other sites

1 hour ago, webaff said:

Robin S, further up berhard proposes to file an issue at github. Do you agree?

I don't think there is any bug to report here - a $pages->find() selector must ultimately become an SQL query and the selector sort options become an ORDER BY clause. You cannot do something in an SQL query like "sort by some column but if that column is empty for a row then use some other column instead".

But I guess you could make a request in processwire-requests in case Ryan can come up with some wizardry to allow OR sorting.

  • Like 2
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...