How to sort Modx getResources in a specified order?

1.7k Views Asked by At

How can we sort a modx getResources call in the order specified in the 'resources' attribute?

I have:

[[!getResources? 
    &parents=`-1`
    &resources=`[[*HomePageUpcomingEvents]]` 
    &tpl=`SecondaryUpdatesHomePageTpl` 
    &limit=`3` 
    &showHidden=`1` 
    &includeContent=`1` 
    &includeTVs=`1` 
    &processTVs=`1`
    ]]

where: HomePageUpcomingEvents is a comma separated list of ids I would like to display in a specific order: 6405, 6154, 6991

1

There are 1 best solutions below

1
On

https://rtfm.modx.com/extras/revo/getresources - read the docs before asking questions.

&sortby=`FIELD(modResource.id, 4,7,2,5,1 )`

In your case, it would be

&sortby=`FIELD(modResource.id, [[*HomePageUpcomingEvents]] )`

UPD

Three examples:

[[!getResources?
    &parents=`-1` 
    &resources=`1,2,3` 
    &sortby=`FIELD(modResource.id, 3,2,1 )`
    &tpl=`@INLINE [[+id]]`
    &showUnpublished=`1` 
    &showHidden=`1` 
    &showDeleted=`1`
]]
<hr>
[[!getResources?
    &parents=`0` 
    &resources=`1,2,3`
    &sortby=`FIELD(modResource.id, 3,2,1 )`
    &tpl=`@INLINE [[+id]]`
    &showUnpublished=`1` 
    &showHidden=`1` 
    &showDeleted=`1`

    &sortdir=`ASC`
]]
<hr>
[[!getResources?
    &parents=`0` 
    &resources=`1,2,3` 
    &sortby=`FIELD(`modResource`.`id`, 3,2,1 )`
    &tpl=`@INLINE [[+id]]`
    &showUnpublished=`1` 
    &showHidden=`1` 
    &showDeleted=`1`

    &sortdir=`DESC`
]]

Three outputs:

1
2
3
<hr>
3
2
1
<hr>
1
2
3