Jump to content

Slightly more advanced multiple sorting


Tom.
 Share

Recommended Posts

Hello, 

I was wondering if there is a simpler way of sorting like this using the API selector:

$items = $page->children("sort=title");
$featured = new PageArray();
$featured->add($items);
$items->prepend($featured->filter("featured=1")->sort("timeon"));

Basically I'm wanting anything that's featured to filter by the time the artist is playing ("TimeOn") and for the rest to filter by title. I know above isn't exactly bloated, but I was just wondering if there is something simpler? Maybe a way of doing this inside the selector. 

This method is quite nice because ProcessWire automatically removes duplicates so it orders perfectly. 

Link to comment
Share on other sites

You can sort multiple like this:

$page->children("featured=1, sort=title, sort=timeon");

Sorry maybe I wasn't clear: 

Any child that is featured=1 will be sorted by the time they are on, and any child that isn't featured will be ordered alphabetically. 

  • Like 1
Link to comment
Share on other sites

I was wondering if there is a simpler way of sorting like this using the API selector:
$items = $page->children("sort=title");
$featured = new PageArray();
$featured->add($items);
$items->prepend($featured->filter("featured=1")->sort("timeon"));

I doubt it makes much (or any) difference in terms of performance but you can do the same a bit more briefly like this:

$items = $page->children("sort=title");
$items->prepend($items->find("featured=1, sort=timeon"));
  • 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...