Jump to content

Sort pages by multiple keys


a-ok
 Share

Recommended Posts

What's the best way to sort two PageArrays by two separate keys using PW? I'm unsure if this is possible rather than using just PHP (but was curious to know if PW had such a way).

I have a page, 'Designers', and I have three fields for the children pages... title, firstname and lastname. If the designer is a person then they fill out all three fields, but if they are a collective/group then they just fill out the title. My plan would be to then create two queries... one returning only the designers with full names (first and last) and then the other returning collective/groups only.

$designers_person = $pages->find("parent=/designers/, designers_lastname!='', sort=designers_lastname");
$designers_group = $pages->find("parent=/designers/, designers_lastname='', sort=title");

What I then need to do is combine these two queries and sort them so there is an alphabetical order, for example...

Anni Albers (person)
Kate Blee (person)
Tord Boontje (person)
Commune (collective)
Javier Mariscal (person)
Studioilse (collective)

Any thoughts?

Link to comment
Share on other sites

$designers_person = $pages->find("parent=/designers/, designers_lastname!=''")->each(function($designer){
	$designer->custom_sort = $designer->designers_lastname;
});
$designers_group = $pages->find("parent=/designers/, designers_lastname=''")->each(function($collective){
	$collective->custom_sort = $collective->title;
});;

$all = $designers_person->add($designers_group)->sort('custom_sort');

 

  • Like 9
  • Thanks 1
Link to comment
Share on other sites

6 minutes ago, LostKobrakai said:

$designers_person = $pages->find("parent=/designers/, designers_lastname!=''")->each(function($designer){
	$designer->custom_sort = $designer->designers_lastname;
});
$designers_group = $pages->find("parent=/designers/, designers_lastname=''")->each(function($collective){
	$collective->custom_sort = $collective->title;
});;

$all = $designers_person->add($designers_group)->sort('custom_sort');

 

This is nuts. I had no idea you could do this. Thank you!

  • Like 1
Link to comment
Share on other sites

Wow, this is great!

I've also got a sorting problem, maybe this could be achieved in a similar way? The problem is this:

I need a list of pages, that contains elements from the respective parent page. Is there a way to sort the list by a field that is only in the parent page? Something like:

$results = $pages->find("template=name-of-child-template, sort=parent->field-on-parent-template");

 

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