How to sort records by desc in blog plugin

I have retrieved records to display on the home page as follows:

[collection blog]
handle = "Blog\Post"
sortDirection = "desc"

{% set posts = blog.whereRelation('categories', 'slug', 'services').get() %}

display list record to homepage:

{% for post in posts %}
            {% partial 'block/service' %}
{% endfor %}

Result: all records with slug=“service” were displayed BUT not sorted in descending order, even though I declared it as sortDirection = “desc”.

So to sort the records in descending order, how do I set it up? If possible please guide me.
Thank you.

Looks like you’re almost there. You just need a sortColumn property

Example:

[collection blog]
handle = "Blog\Post"
sortDirection = "desc"
sortColumn = "title" #or whatever you need
2 Likes
[collection blog]
handle = "Blog\Post"
recordsPerPage = 5
sortColumn = "created_at"
sortDirection = "desc"

I added it but the result did not change.

What about something like this ?

[collection blog]
handle = "Blog\Post"
sortDirection = "desc"

{% set posts = blog.whereRelation('categories', 'slug', 'services').orderBy('published_at', 'desc').get() %}

{% for post in posts %}
    {% partial 'block/service' %}
{% endfor %}

2 Likes

This is a great way. Thank you for your support.