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.

Share this post

Link to post
Share on other sites

Seems like a great idea -- I just now added it in the latest commit. Now it should support the "sort=template".

  • Like 1

Share this post

Link to post
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.

Share this post

Link to post
Share on other sites

@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 ;)

Share this post

Link to post
Share on other sites

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;


Share this post

Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By celfred
      I've just upgraded to 3.0.165 (and updated my Ubuntu version as well) and on my localhost, I am facing a weird issue : all my requests having the ~= selector cause a Mysql error with this message :

      PDOException #HY000 SQLSTATE[HY000]: General error: 3685 Illegal argument to a regular expression. I have no idea what is going on. If I change my request from

      $visualizer = $pages->get("name~=visualizer"); // Triggers the error
      $visualizer = $pages->get("name=visualizer");
      $visualizer = $pages->get("name~*=visualizer");
      My code works fine again.
      Any idea ? Shall I change all my requests (but from what I understand by reading the documentation, ~= exists and fits my needs : all words in any order [though I do understand that in my example above it may be useless since I have only one word])
      Thanks !
    • By Brian Peat
      Hello! I've got a single client on Processwire and it's completely foreign to me. I've been able to find most things, but they've asked for a custom designed page with a full width header image/section, and then a body block and a right side bar below the header. They're using a landing page template, so I duplicated that, figured out how to add it in the admin, and assigned it to the page. Nothing. It doesn't change a thing.
      I feel like I'm missing something obvious. I've made sure all the fields are the same, I tried to set up the parent/child stuff though this page doesn't have a parent that I can tell (though it's under Landing Pages). If I can get the template to actually kick in, I think I can use a bit of code to check for the hero section and load it at the top. I also made a copy of the widget template and set that to load instead of the original. But again, since my main page template isn't kicking in, neither is the new widget template.
      I'd just love some tips on what to check or change to get a landing page to actually render what's in the assigned page template.
      Hopefully I've used the correct terminology here to make sense.
    • By rookie
      hi there,
      Is there a module or other solution to select a template with a preview image? Sometimes it is not that easy to describe a template.
      e.g. Template with 3 columns (image, title, content, button) or template for a slider with max. 4 pictures.
      Even if it is nice to have a description field for a template, a picture says more than thousand words could describe.
      And no, I don't think I could develop something like that. I don't even know where to start.🙄
    • By muzzer
      I have just upgraded a site from 2.7 to latest v3.
      I have run into a weird issue where a page template setting for slashUrls changes from No to Yes immediately after an ajax call, causing the second ajax call on the page to use the wrong setting.
      There is no reference to the $template->slashUrls() in any of my code.
      This happens every time I load the page, it's 100% consistent.
      I have this running on a second machine under v2.7 and no such weirdness.
      I'm lost as to why this is happening, what would cause a PW setting change for slashUrls? Anyone have any ideas?
    • By Pip
      Hi everyone, 
      Is there a way for us to replicate the "http://mydomain.com/processwire/page/edit/?id=xxx" and change the template to match my site template? 
      I'm terribly happy with the admin / backend page edit. It covers literaly everything I want to empower my non super admin user in updating pages such as validation, repeater management, file upload. 
      I wish not to allow the users to see the backend for both security and aesthetic reasons. 
      Thanks and hope to hear from you soon. 
  • Create New...