Sorting Events and using Query API

Phillip Coxwell's Avatar

Phillip Coxwell

17 Jul, 2015 04:01 PM

I am attempting to use the Query API to pull current and past events, but I am running into an issue with the sorting. I want to have the events on each page sorted with the closest events first. I am not using the Start Date or End Date since they affect the publishing of the event pages, and sorting by the creation date also doesn't always work, so that leaves me kind of in a pickle. Based on what I have read so far, there appears to be no way to sort by a structuredDataNode, so I'm wondering if what I am attempting to do is even possible.

  1. 1 Posted by Ryan Griffith on 20 Jul, 2015 12:25 PM

    Ryan Griffith's Avatar

    Hi Phillip,

    You are correct, the Query Tool currently only supports sorting by wired metadata fields, so you won't be able to use the tool to sort your events. I believe we have an improvement logged internally for such a feature, though.

    That being said, your best bet would be to loop over the results of the Query Tool and generate a new array of objects that contains a key for your structured data field and use the Sort tool to sort the new array. Something like the following:

    ## Grab the events using the Query Tool
    #set ( $events = ... )
    #set ( $eventsToSort = [] )
    
    #if ($events.size() > 0)
      #foreach ($event in $events)
        $!eventsToSort.push({
          "startDate": $event.getStructuredDataNode("startdate").textValue,
          "pageObj": $event
        })
      #end
    
      #foreach ($event in $_SortTool.sort($eventsToSort, "startDate:desc"))
        #set ( $page = $event.get("pageObj") )
        ## ... Output events ...
        $page.metadata.title
      #end
    #end
    

    Please let me know if you have any questions.

    Thanks!

  2. Tim closed this discussion on 12 Aug, 2015 01:40 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM