Event posts grouped by Month
For Advanced Views Pro users to help you through step-by-step on how to display events or posts grouped by the date field.
Last updated
For Advanced Views Pro users to help you through step-by-step on how to display events or posts grouped by the date field.
Last updated
<?php
use Org\Wplake\Advanced_Views\Pro\Bridge\Cards\Custom_Card_Data;
return new class extends Custom_Card_Data {
public function get_variables(): array
{
$post_ids = $this->get_default_variables()["_card"]['post_ids'] ?? [];
$grouped_items = [];
foreach ($post_ids as $post_id) {
// declare the field to group by
$raw_date = get_field('date', $post_id); // ACF date field
// convert to month string
$month = "";
if ($raw_date) {
$timestamp = strtotime($raw_date);
$month = $timestamp ? date('F Y', $timestamp) : 'No Date';
} else {
$month = 'No Date';
}
// initialize group if not exists
if (!key_exists($month, $grouped_items)) {
$grouped_items[$month] = [];
}
// add post to group
$grouped_items[$month][] = $post_id;
}
return [
"grouped_items" => $grouped_items,
];
}
};// Card template example
<acf-card-69a3ee57020ca class="{{ _card.classes }}acf-card acf-card--id--{{ _card.id }}">
{% if _card.post_ids %}
<div class="acf-card__items">
{% for date, post_ids in grouped_items %}
{{ date }}
<hr>
{% for post_id in post_ids %}
[avf-layout id="{{ _card.view_id }}" object-id="{{ post_id }}"]
{% endfor %}
{% endfor %}
</div>
{% else %}
<div class="acf-card__no-posts-message">{{ _card.no_posts_found_message }}</div>
{% endif %}
</acf-card-69a3ee57020ca>