0

Wordpress site build on Bedrock needs template adjustment. Events displayed in the template are sorted from the oldest to the newest. I assume this is default behavior, as I can't see any specific part responsible for sorting.

I would like to sort events from the newest to the oldest.

Code in the template for displaying events:

  <list-posts
      :list="{{ $past_events }}"
      api-url="/wp-json/version/past-events"
    >
      <template v-slot:default="{ list }">
        <event-card
          v-for="event in list"
          :key="event.id"
          :event="event"
          type="overview"
        >

I tried to add different sorting command to the end of "wp-json..." path:

?filter[orderby]=date&order=desc
?filter[orderby]=event_date&order=desc
?orderby=date&order=desc

but they break the site (each line used separately).

I would appreciate any hint on how to correctly sort events (newest to oldest).

yolosz
  • 1
  • 2

2 Answers2

0

Create a computed property and sort the array there.

https://v2.vuejs.org/v2/guide/computed.html

<div id="example">
  <p>Original message: "{{ message }}"</p>
  <p>Computed reversed message: "{{ reversedMessage }}"</p>
</div>
var component = new Vue({
  el: '#example',
  data: {
    message: 'Hello'
  },
  computed: {
    // a computed getter
    reversedMessage: function () {
      return this.message.split('').reverse().join('')
    }
  }
})

You might be able to sort this only once and store in the data object if you wanted to try and be more efficient.

tony19
  • 125,647
  • 18
  • 229
  • 307
James Webb
  • 167
  • 2
  • 17
0

Thank you for your suggestion.

I also found out that I can get reversed array by just using:

 v-for="event in list.slice().reverse()"

This is the related answer: vue.js: reverse the order of v-for loop

yolosz
  • 1
  • 2