Grouping posts by date?

Hi all,

Has anyone figured out a way to “group” posts by date within a {{foreach}} loop?

Right now I’m presenting my posts sorted by date in descending order, and printing the “published on” date below the title of every post.

What I’d like to do is only print each date once as a “heading,” thereby grouping the posts from the same day together, e.g.:

June 26, 2019

{{post 1 title}}
{{post 1 body}}

{{post 2 title}}
{{post 2 body}}

June 25, 2019

{{post 3 title}}
{{post 3 body}}

June 24, 2019

{{post 4 title}}
{{post 4 body}}

I’ve looked at the “if” “date” and “foreach” portions of the docs, but haven’t been able to figure out a way to encode “only print the date if it is different from the last post” into my theme.

Any suggestions or ideas? Is there another way I should be trying to accomplish this?

Thanks!

Chris

1 Like

from Post Archive - Fizzy :slight_smile:

// =================================================
// post archive: add year and month break 2019.05.28
// =================================================
// Year & Month Break
var yearArray = new Array();
var monthObj = new Object();
$(".post-archive-item").each(function() {
    var archivesYear = $(this).attr("year");
    var archivesMonth = $(this).attr("month");
    yearArray.push(archivesYear);
    if (archivesYear in monthObj) {
        monthObj[archivesYear].push(archivesMonth);
    }
    else {
        monthObj[archivesYear] = new Array();
        monthObj[archivesYear].push(archivesMonth);
    }
});
var uniqueYear = $.unique(yearArray);
for (var i = 0; i < uniqueYear.length; i++) {
    var html = "<hr><h2>" + uniqueYear[i] + "</h2>";
    $("[year='" + uniqueYear[i] + "']:first").before(html);
    var uniqueMonth = $.unique(monthObj[uniqueYear[i]]);
    for (var m = 0; m < uniqueMonth.length; m++) {
        var html = "<h4>" + uniqueMonth[m] + "</h4>";
        $("[year='" + uniqueYear[i] + "'][month='" + uniqueMonth[m] + "']:first").before(html);
    }
}
                                {{#get "posts" limit="all" include="authors" order="published_at desc"}}
                                    {{#foreach posts}}
                                    <li class='post-archive-item' year="{{date format=(t 'YYYY')}}" month="{{date format=(t 'MMMM')}}">
                                            <div class="date">
                                                <time class="button is-static is-size-7">{{date published_at format=(t "MMM DD")}}</time>
                                            </div>
                                            <div class="subtitle">
                                                <a href='{{url}}'>
                                                    {{title}}
                                                </a>
                                                {{#if featured}}
                                                    <span style="color:var(--main-color)" title="{{t "Featured"}}"><i class="iconfont icon-star"></i></span>
                                                {{/if}}
                                                <span class="authors"> - {{#authors}} {{name}} {{/authors}} </span>
                                            </div>
                                    </li>
                                    {{/foreach}}
                                {{/get}}