Jump to content

sorting by admin order after getting pages by template


chodorowicz
 Share

Recommended Posts

I'll admit right away that I've managed to solve it, but maybe someone else will find this useful and also I believe it would be good to put it somewhere in FAQ/Documentation

When getting pages by template like so:

$categories = $pages->find("template=category");

the results are not sorted according to menu order (maybe it's a bug?). I couldn't find anywhere the info how to force the sort by menu order (everywhere in the documentation there are examples with title and some other variables).

The solution was quite easy, but I had to look to database and source code:

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

Btw. thanks for this amazing CMS - I've spent several hours today learning and doing my first website and I must say it is impressive. It has great potential.

Cheers.

  • Like 1
Link to comment
Share on other sites

Hi chodorowicz,

welcome to the forums.

I agree, 'sort=sort' should be in docs and also there should be info about what sorting is used by default (My personal guess is that it's sorted by internal ID).

Also, you can use 'sort=-sort' if you want to reverse the order.

Link to comment
Share on other sites

Good points.

The "sort=sort" is assumed when you use the children() or siblings() functions. But for find() operations, a relevancy sort is introduced. Of course, if your find() doesn't involve some kind of keyword, then that relevancy doesn't really come into play. I think what may be appropriate is for me to automatically add a "sort=sort" to any find() operations that include a "parent=" portion of the selector. What do you think?

  • Like 1
Link to comment
Share on other sites

This is covered on the documentation in the API > Selectors section, but wasn't previously at the level of detail you requested. The possible values depend on what fields you've created (they are the values). But I didn't cover the "sort=sort" before, so that's taken care of now. Here is what I added:

How results are sorted if you don't specify a "sort" in your selector

In $page->children() and $page->siblings() the results are automatically sorted by the page's default sort field that you specify in the admin. If not specified in the admin, the pages will be sorted by the order they are placed in the admin. This behavior can be overridden by specifying your own "sort=[property]". With $pages->find() and $page->find(), if you don't specify your own "sort=[property]", the results are sorted according to MySQL's text searching relevance. If no text searches are performed in your find(), the results are unsorted. As a result, it is generally a good idea to include a "sort=[property]" when using $pages->find(), especially if you care about the order and your find() operation is not text/relevance related.

How to force pages to sort by their admin order with $pages->find()

Unlike $page->children(), the $pages->find() function does not automatically sort by the order they appear in the site tree. This is because $pages->find() is not limited to finding pages specific to one parent, so it may be pulling pages from multiple places (according to your selector). If your parent page(s) are not already sorting by a specific field, 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.

  • Like 1
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...