Ad

How Can I Order By Date Using Twig For Within Seperate Blocks With Date Fields Within A Matrix Field?

- 1 answer

For example I have a property matrix with 2 fields. Buy and Rental.

Within Buy and Rental I have a date field as well as various other fields like a heading etc.

I would like to order ALL of the properties by date - the value of the date field in each block.

Ad

Answer

Since we don't have any code to go by I will give it a shot in the dark anyway.

In your template you could use some Craft queries. For a category query it would look like this (entries could be the same as well):

{% set categories = craft.categories().orderBy('dateCreated asc').all() %}

All this does is fetch the categories, then orders them by the date created in ascending order. You can change dateCreated to postDate and asc to desc if you need to.

If you need a specific category group you would use {% set categories = craft.categories().group(myCategoryGroup).orderBy('dateCreated asc').all() %}

Instead of running the above you could also run:

{% set categories = craft.categories().orderBy('dateCreated asc').all() %}
{% set cateByDate = categories|group('postDate|date("Y")') %}

Again, same concept just we are querying by a specific group of categories.

Next you will have to setup a loop to iterate through the categories.

{% for category in categories %}
<p>{{ category.dateCreated|date("M/d/Y")}}</p>
{% endfor %}

This is a simple overview of how the problem can be approached, but we don't have code to be able to help more. There are many different possibilities.

Ad
source: stackoverflow.com
Ad