Jump to content

Sort by repeater count


Tom.
 Share

Recommended Posts

I'm having trouble with sorting by repeater count. We have awards given to each work and the awards are in a repeater. Doing "sort=-awards.count"

However it isn't working. Anyone got any ideas?

 

Link to comment
Share on other sites

Don't know if it should be possible to do this or not, but one workaround might be to have a field on the page to hold the count, and update it automatically on page save. You can set the field (say awards_count) to not show in the admin, so it will only be maintained by a simple hook function. It's really easy to do...

Add a new field awards_count to the appropriate template (I'll assume work based on your question) and set it to not show in the admin (Admin > Fields > awards_count > Input > Visibility > Hidden (not shown in the editor)

Add a hook in /site/templates/admin.php something like this

<?php namespace ProcessWire;

/**
 * Admin template just loads the admin application controller,
 * and admin is just an application built on top of ProcessWire.
 *
 * This demonstrates how you can use ProcessWire as a front-end
 * to another application.
 *
 * Feel free to hook admin-specific functionality from this file,
 * but remember to leave the require() statement below at the end.
 *
 */

 $pages->addHook('saveReady', function($event) {
   $pages = $event->object;
   $page = $event->arguments(0);
   if($page->template == 'work') {
     $page->awards_count = $page->awards->count();
   }
 });

require($config->paths->adminTemplates . 'controller.php');

And that should do it! Then you can do sort=-awards_count using the new field instead of the count property.

  • Like 1
Link to comment
Share on other sites

12 hours ago, Tom. said:

I'm having trouble with sorting by repeater count.

I can confirm that sorting by repeater count does not work. Seems like a bug to me, considering that it's possible to sort by Page Reference count and PageTable count and those are quite similar fieldtypes. @Tom., maybe open an issue on GitHub?

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

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

×
×
  • Create New...