Jump to content
a-ok

Change order of Wire/PageArray based on a pageField

Recommended Posts

Is it possible to sort a Wire/PageArray (I'm using the RepeaterMatrix) by a field's value? The order is defined by the order in the CMS but if I wanted to change that order, on the front-end, to follow a pageField selection is this possible?

Each row has a pageField that allows the client to select a number of categories that defines that row's content. If the client selects 'Digital' for a row then is there a way, on the front end, to put all the rows with that category first (thus breaking the order)? I would then use a get request (?f=digital) to define when the order should change, but I'm just wondering if the sorting order change is possible.

I could probably loop through the rows and remove/append but wondered if this was possible within a selector?

Share this post


Link to post
Share on other sites
// find pages and sort
$pages->find('sort=firstsortfield,sort=secondsortfield');

// Sort WireArray from newest to oldest
$items->sort("-created");


// Sort WireArray by last_name then first_name
$items->sort("last_name, first_name");

https://processwire.com/api/ref/wire-array/sort/

Share this post


Link to post
Share on other sites

I ended up with...

$modules = $page->modulesA;

if ($input->get('f')) {
    $moduleFilter = $input->get('f');
    $modulesWithFilter = $modules->find("projectsCategories.name={$moduleFilter}");
    $modulesWithoutFilter = $modules->removeItems($modulesWithFilter);
    $modules = $modulesWithFilter->add($modulesWithoutFilter);
}

 

Share this post


Link to post
Share on other sites

I recommend to sanitize the input
 

$moduleFilter = $sanitizer->pageName($input->get('f'));

 

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.

×
×
  • Create New...