Jump to content

Sort the selectors results by template name


Recommended Posts

I am wandering to find out if it is easy something like:

"..., sort=template, ..."


"..., sort=template.name, ..."

for sorting the pages by their template name in selectors result.

When it will be useful:

1. My clients were asking to display the professors first, then doctors, then students in the paginated staff list.

2. Other clients requested to display products pages first in the site search results.

I know I could use two search selectors and to concatenate their PageArrays but it will be more complicated with the pagination. I could use a dropdown with "search in products", "search in site" too.

Link to comment
Share on other sites

Great to have this. Thanks guys. Just to verify, it will "group" and sort by template name? So the problem of having a specific order would only be able with naming the templates according 01-tplx, 02-tply, 03-tplz? Since templates can also have labels this won't be much of a problem I guess.

Link to comment
Share on other sites

  • 4 weeks later...

@Soma - I guess that could work, but this is probably better to use just to group the templates together and then parse the PageArray and set correct order, so you don't run into problem with different orders needed with 'XX-template' system.

Example: on search, you need 01-tpl1, 02-tpl2, 03-tpl3. On a different listing, you'll need 03-tpl3, 01-tpl1, 02-tpl2 in this order, and then you're screwed. But this is just a note, you may not run into this issue (if you have one search only, for instance ;)

Link to comment
Share on other sites

  • 4 years later...

Here's my solution for sorting pages by template, with a sort order:

// Templates listed in sort order
$templates = ['template1', 'template2', 'template3'];

// Get pages with templates
$results = $pages->find('field=value, template=' . implode('|', $templates));

// Sort results by template
$resultsSorted = new \PageArray();
foreach ($templates as $t) {
    $resultsSorted->import($results->find('template=' . $t));

// Reassign to $results
$results = $resultsSorted;


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

  • Create New...