We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14162
    • 67 Posts
    getResources can sort by menuindex - which is great for items in one folder/container. However, I need to sort the children of several folders in one overall order, i.e:

    Folder 1
    - item 1
    - item 2
    Folder 2
    - item 3
    - item 4
    Folder 3
    - item 5
    - item 6

    To display: item 1, item 2, item 3, etc.

    Because the menuindex value is defined by a resource’s position in its parent container, the getResources &sortby=`menuindex` does not work for multiple folders. You end up with loads of resources with the same menuindex value, so the sort order is more or less random. Sorting by `publishedon` works because this is not defined by the container. Trouble is, I don’t want to sort in publishedon order - I need to sort in the order the resources appear in the resource tree.

    So my question is: is there a way to sort child resources of multiple folders based on their position in the resource tree

    For reference, my current code is:
    [[getResources? &parents=`[[*parent]]` &depth=`2` &resources=`-[[*id]]` &limit=`` &hideContainers=`1` &includeTVs=`1` &sortby=`menuindex` &sortdir=`ASC` &tpl=`product_list` ]]


    [ADDENDUM]
    One obvious way round this issue is to manually change all the menuindex values so they run sequentially down the resource tree, but then I would have to amend this every time a new child resource was created or moved.

    Another option would be to create a snippet that first sorts on the parent’s menuindex and then on the child’s - but I don’t really know how to go about doing this. Any takers?
      • 22303 MODX Staff
      • 10,725 Posts
      This is what Wayfinder is good at, making hierarchical navigation lists. getResources is not intended to do this. It’s for summarizing flat lists of Resources.
        • 14162
        • 67 Posts
        Understood - and I use Wayfinder for my site navigation. However, all I need is a flat list of items, but sorted in the order they appear in the resource tree (irrespective of the folders they are in).

        I thought I could maybe use getResourceField to pull in the parent folder’s menuindex and then append the item’s menuindex, creating a larger number (e.g. parent menuindex of 1 and a child menuindex of 3 = 13). But I can’t make getResourceField pull in the parent menuindex as part of the getResource call:
        [[!getResources? &parents=`[[*parent]]` &depth=`2` &resources=`-[[*id]]` &limit=`` &hideContainers=`1` &includeTVs=`1` &sortby=`[[getResourceField? &id=`[[*parent]]` &field=`menuindex`]]` &sortdir=`ASC` &tpl=`product_list` ]]


        That just pulls in the menuindex of the parent of the page where the getResource snippet is running from. Any idea on how to tweak this to pull in the parent’s menuindex for each item?

        Ps. Would Wayfinder even be able to list the items in resource tree order?
          • 14162
          • 67 Posts
          I’ve tried Wayfinder and it can’t reproduce what I want. It doesn’t seem to be able to exclude containers - which is crucial as I don’t want the folders to appear in my listing, but I do want them to remain unhidden so they can appear in the wider site navigation. Besides, it seems like a bit of overkill for what is a pretty simple task. I’m sure there’s a way of tweaking getResources to manage it... I will continue trying.
            • 7557
            • 61 Posts
            Quote from: jimbob72 at Mar 15, 2011, 12:51 PM

            I’ve tried Wayfinder and it can’t reproduce what I want. It doesn’t seem to be able to exclude containers - which is crucial as I don’t want the folders to appear in my listing, but I do want them to remain unhidden so they can appear in the wider site navigation. Besides, it seems like a bit of overkill for what is a pretty simple task. I’m sure there’s a way of tweaking getResources to manage it... I will continue trying.
            Try this:
            http://modxcms.com/forums/index.php/topic,43832.msg333619.html#msg333619
              • 29019 ☆ A M B ☆
              • 104 Posts
              You could always nest your getResources call as well. Set the depth on your getResources call to 0, then in your chunk, put another getResources call, so it would be something like this:

              [[+pagetitle]][[getResources?
              	&tpl=`your-chunk`
              	&depth=`0`
              	&parents=`[[+id]]`
              	&sortby=`menuindex`
              	&sortdir=`desc`]]
                www.darkstardesign.com

                Are you in the Nashville area? Join us for our next MODX meetup!
                • 14162
                • 67 Posts
                Great solution dj13. Now working perfectly. Many thanks.
                  • 12512
                  • 42 Posts
                  I also would like to see a tree-wise sort order for getResources. WayFinder cannot be used for my application because it does not support a filter like tvFilters to list a resource tree filtered by keywords.

                  Also chaining the getResources calls is not an option for my application either as again the tvFilters parameter cannot be passed on to the second level getResources call.

                  Currently I am using the getResourcesTree filter snippet recommended by dj13 but it would be nice to see this integrated into getResources somehow.
                    • 19604 ☆ A M B ☆
                    • 179 Posts
                    Quote from: hwmaier at May 09, 2011, 06:29 PM

                    I also would like to see a tree-wise sort order for getResources. WayFinder cannot be used for my application because it does not support a filter like tvFilters to list a resource tree filtered by keywords.
                    That’s one of my favorite wishes, too! sortby=`tree`!
                      MINDEFFECTS – DESIGN for PRINT, WEB and MEDIA
                      http://twitter.com/mindeffects · http://www.facebook.com/mindeffects · http://www.youtube.com/mindeffects/ · skype://mindeffects_oliver
                      • 22303 MODX Staff
                      • 10,725 Posts
                      Quote from: hwmaier at May 09, 2011, 06:29 PM

                      Also chaining the getResources calls is not an option for my application either as again the tvFilters parameter cannot be passed on to the second level getResources call.
                      Why can you not pass tvFilters to the second level call? It should be as easy as sharing a PropertySet across all your getResources calls with the same tvFilters value set.