Jump to content
Sign in to follow this  
Nils Wiere

Sorting and subfolders

Recommended Posts

This must be simple, however, I couldn't figure out how to do it. I got this structure in the admin tree:

Page 1 [Template A]

    Children 1-1 [Template B]
    Children 1-2 [Template B]
    Children 1-3 [Template B]
    ...

Page 2 [Template A]

    Children 2-1 [Template B]
    Children 2-2 [Template B]
    ...

Page 3 [Template A]

    Children 3-1 [Template B]
    ...

What I want in the front end, is this sort order:

Children 1-1
Children 1-2
Children 1-3
Children 2-1
Children 2-2
Children 3-1

Plus, the order should depend on the order in the admin tree. So, if I move Page 3 on top of the tree, the children of Page 3 should appear first.

I tried this, but the order seemed to be random (without a connection to the order in the admin tree):

$pages->find("template=B, sort=sort");

I also tried this, and again I got a random looking order:

$pages->find("root.template=A, sort=sort");

Do you have any idea?

Thanks,
Nils

Share this post


Link to post
Share on other sites

Hi @Nils Wiere

Try this:

$pages->find("template=B, sort=parent.title, sort=sort");

or this

$pages->find("template=B, sort=parent.sort, sort=sort");

Not sure that this will work, but give a try.

As alternative:

$parents = $pages->fint("template=A, sort=sort");
$items = new PageArray; 
foreach($parents as $parent) {
	$items->import($parent->children("sort=sort"));
}

 

  • Like 6

Share this post


Link to post
Share on other sites

I knew there's an easy solution (as always in ProcessWire) – thanks Zeka!

I wasn't aware that we have a sort option "parent.sort".

So for me, this was the solution:

$products = $pages->find("template=B, sort=parent.sort, sort=sort"); 

  • Like 2

Share this post


Link to post
Share on other sites
3 minutes ago, Nils Wiere said:

I knew there's an easy solution (as always in ProcessWire)

^-^

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By schwarzdesign
      I'm working on a news feed that will show the most recent news in a full teaser grid, and all older news as a simpler archive-type list view. My selectors so far:
      $news_full = $page->children("template=news, limit={$page->feed_count_full}"); $news_archive = $page->children("template=news, start={$page->feed_count_full}, limit=9999"); The $page->feed_count_full field controls how many items to show in the teaser grid (I've confirmed it contains the correct value, and the $news_full selector works as intended).
      This works, but I don't like the limit in the second selector. Unfortunately, if I leave it out (i.e. I only specify a start, not a limit), the start is ignored and I get all news instead. Not a big problem as we will never have more than 9999 news, but it still bothers me, as semantically speaking I don't want to set a limit in this case.
      Is this the intended behaviour of start/limit selectors? Is there a cleaner way to specify an offset (start selector) without a limit?
      ProcessWire Version 3.0.123
      Thanks!
    • By pwFoo
      I played with the Selectors object and would like to use it for a special use case...
      That is a custom Selectors object with dummy data converted to an php array to see the structure (Selectors object is a WireArray with "fields" added)
      Array ( [0] => Array ( [0] => Array ( [field] => seg1 [value] => val1 [not] => [group] => [quote] => [forceMatch] => ) [1] => Array ( [field] => seg2 [value] => val2 [not] => [group] => [quote] => [forceMatch] => ) ) )  
      But instead of "seg1" the field name is "field" with value "seg1" (= my field name).
       
      So I can't search the Selectors WireArray (= custom WireArray with added Selectors objects) with PW "find('seg1=val1')", Is there a way to search with "find()" or build a simple wrapper to make the elements searchable / filterable with find()?
    • By louisstephens
      I have done a bit of searching, but I can not seem to find an actual answer. I have a list of services as child pages under "Services". I can output the services just fine, but I cant wrap my head around how to group them "alphabetically" like:
      Services A - Service "A" 1 - Service "A" 2 - Service "A" 3 B - Service "B" 1 - Service "B" 2 - Service "B" 3 C - Service "C" 1 - Service "C" 2 - Service "C" 3 Has anyone achieved this type of functionality before?
    • By Peter Knight
      <?php $Cats=$pages->find("parent=1086, id!=$page, sort=sort"); foreach($Cats as $Cat) { echo " I have a selector that pulls in 4 child pages of a parent and the order is set to reflect the order of the tree.
      My client has asked that instead of
      Cat 1 | Cat 2 | Cat 3 | Cat 4
      we instead display
      Cat 4 | Cat 1 | Cat 2 | Cat 3 
      However, I can't change the sort order in the tree.
      Can anyone guide me on the correct approach here?
      Thanks
       
    • By theoretic
      Hi there! And thanks for Processwire!
      It appears that i've found something interesting about PW selectors. They should only be strings! Here's an example of SQL-like syntax for selector:
      $my_complex_selector = " name='some name', parameter=123, other_parameter=[subparam>=subvalue] "; Trying to use this selector lead to a very buggy PW behaviour. It appears that newlines are treated in a very special manner by PW selector engine, preventing the newlined selectors from working as expected.
      I cannot imagine a situation when an unescaped newline could be a part of selector or selector value, so stripping newline symbols from selector could be a good idea for further PW development. And, currently, another good idea is to write complex selectors as PHP arrays:
      $my_complex_selector = [ "name=$name", "param1=$param1", ]; and to implode them into a single line before using find() and other functions which use selectors.
      Sorry if i wrote something trivial, but having this post already present at support forum could save me a couple of hours. Hope mine will save that tame for someone else 😉
×
×
  • Create New...