Jump to content

Sorting results of find() by admin order


Lars282
 Share

Recommended Posts

Hey!

I just tried this:

... you may still tell the $pages->find() to sort by the parent's order by specifying "sort=sort" in your selector. This is the same as saying "sort by whatever order I dragged the pages to in the admin." But note that if the results have multiple parents, the resulting order isn't likely to be useful.

Result: http://aglobalvillage.org/journal/global-health/

The grey bars are the titles of the parent pages (to be exact, the parents of parents) ... somehow this does not seem to be sorted as in the admin ... ?? If it was as in admin, surely they should be grouped by common parents?

How can I group all the results by the parent pages? I.e. how can I make the find() return the pages as they appear in admin order?

Thanks,

Lars

Link to comment
Share on other sites

The intended result is that above is displayed ordered by say Issue 7, then Issue 6 etc ... and not in apparently random order.

Code:

 foreach ($pages->find("$page->topic_name=1") as $p) {
//Check wether the parent title from last one was different. If so, display new heading.
  if ($i != $p->parents->get("template=issue")->title) {
$i = $p->parents->get("template=issue")->title;
echo '<div class="topic-issue">'.$i.' – '.$p->parents->get("template=issue")->issue_date.'</div>';
  }
  echo '<div class="title"><a href="' . $p->path . '">' . $p->article_title;
  if ($p->article_subtitle) {
echo ': ' . $p->article_subtitle . '</a></div>';
  }
  else {
echo '</a></div>';
  }
  echo '<div class="author">' . $p->article_author_name . ', ' . $p->article_author_affiliation . '</div>';
  }
 unset($p);

How can I order this by template=issue as in admin and then children as in admin?

Screenshot of my page tree iw3l6q.png

Link to comment
Share on other sites

Well, I think you have to itereate twice. First for the parents (issues), then for the articles. Like (pseudocode)

$issues = $pages->find("template=issue,sort=sort")
foreach $isssues as $issue{
 echo $issue->title;
 $articles = $issue->find("topic_name=1,sort=sort")
 foreach $articles as $article
   echo $article->article_subtitle;
 }
}
  • Like 1
Link to comment
Share on other sites

Since the structure is already there, you probably want to use that structure and you won't need to worry about sort order at all, since the sort order is implicit in the structure. I'll adapt MadeMyDay's good example towards the structure from your screenshot. This may not be exactly what you are trying to achieve, but maybe it's close. But the point I'm trying to get across is just that it's better to use this structure for retrieving pages when your output results need it. It's also more efficient to use the structure rather than issuing separate find() queries to pluck stuff out of it arbitrarily.

$issues = $pages->get("/journal/")->children(); 
foreach($issues as $issue) {
   echo $issue->title . '<br />';
   $articles = $issue->children();
   foreach($articles as $article) {
       echo ' - ' . $article->title . '<br />';
   }
}
Link to comment
Share on other sites

It's also more efficient to use the structure rather than issuing separate find() queries to pluck stuff out of it arbitrarily.

hehe, when posting my answer the structure screenshot wasn't there. If there are only children of a kind it is easier/better to use ->children() ;)

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

I currently have a problem with sorting (PW dev version 2.3):

I plan to show a submenu (children of rootParents as long as they have a "menu_name" set):

$children = $page->rootParent->children("menu_name!='', sort=sort");

...does not sort correctly in the order the children are in the page menu.

$children = $page->rootParent->children();

...sorts correctly!

The selector seems to screw up the sort order? Or is there a problem with my code?

Link to comment
Share on other sites

You actually don't need or want the "sort=sort" at all when it comes to a children() call, because it's already assumed, given that parent pages already specify a sort. You would only specify a sort if you wanted to modify the default sort. But there would be no condition under which you'd need to specify a sort=sort to a children() call simply because if the default sort is something other than sort=sort, then "order in the admin" doesn't exist by definition. I don't know why specifying it is breaking it without looking into the code (not in front of it at the moment), but my guess is that it's canceling the default behavior in this case. As a result, I think if you simply omit that part, it should work the way you want. 

  • Like 3
Link to comment
Share on other sites

  • 3 years later...

Sorry for reactivating this old thread, but my current issue seems to fit here...

I have a view with a table of orders. The items can be filtered and sorted, and are returned as a paginated result. Each order item has a property "order_status", which is of type "FieldtypeOptions". The sort order of the options differs from the IDs.

To get the order pages, I am using something like

$pages->find("parent=/orders, modified>=1472680800, limit=15, sort=-order_status");

This sorts by "order_status.id", but i would lilke to sort by "order_status.sort". However, I tried

$pages->find("parent=/orders, modified>=1472680800, limit=15, sort=-order_status.sort");

but this seems to have no effect.

Any ideas how this can be achieved? Am I missing something obvious? Thanks!

Link to comment
Share on other sites

@choppingblock, this does seem to be a bug. I opened a GitHub issue for it.

You can successfully sort a page array according to the sort order of an Options field...

$my_page_array->sort("my_options_field.sort");

...but that perhaps won't help you as I see you want to apply a limit/pagination and maybe don't want to load all the pages into memory.

Maybe you could switch from an Options field to a Page field?

Link to comment
Share on other sites

Yes, a page would definitely have been the better choice... But the project is already live, so it's too late to change that.

Unfortunately, I need the sorting before getting the PageArray. I hoped the parameters would work similar in the find() method...

I'll check for updates then, maybe it will work in a future release.

Thanks!

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...