-
spixels
Hi there,
Does anyone know a way on how to group the query loop output per month based on a post meta date field?
I’ve created a query loop to show events from The Event Calendar, but we would really like to group those per month based on the number of events found and the _EventStartDate post meta of The Events Calendar.
I prefer to take the route above.
Another way I was thinking was creating 2 query loops:
1. Show all posts with the _EventStartDate of this month.
2. Show all posts with the _EventStartDate of next month.I could then add a heading above saying “This month’s events:” and “Next month’s events:”.
Does anyone know a way to do this?
Kind regards,
Sven Kersten -
Hi Sven,
You can try your 2nd method, create 2 query loop block, and then follow the below steps:
1. Add an additional CSS class to the Grid block of the first Query loop block, eg.
this-month-events
, then do the same thing to the second Query loop, eg.next-month-events
.2. Add this PHP code:
//for this month's events query loop add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'this-month-events' ) !== false ) { $meta_query = array( array( 'key' => '_EventStartDate', 'value' => date( 'n' ), // set the value to the current month number 'type' => 'NUMERIC', ), ); $query_args = array_merge( $query_args, array( 'meta_query' => $meta_query, ) ); } return $query_args; }, 10, 2 ); //for next month's events query loop add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'next-month-events' ) !== false ) { $meta_query = array( array( 'key' => '_EventStartDate', 'value' => date( 'n' ) + 1, // set the value to the next month's number 'type' => 'NUMERIC', ), ); $query_args = array_merge( $query_args, array( 'meta_query' => $meta_query, ) ); } return $query_args; }, 10, 2 );
Adding PHP: https://docs.generatepress.com/article/adding-php/
-
spixels
Hi Ying.
Thanks for your efforts. Unfortunately, this code is not working for me.
Both lists are now not showing any events at all…
When I remove the class from the grid block the posts are showing again.
So, somewhere in the code, it doesn’t seem to work 🙁
-
spixels
I’ve asked ChatGPT to fix this for me. Thanks for putting me in the right direction.
Problem is now resolved with the following code:
// Event list this month only add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'current-month-events' ) !== false ) { $first_day = date( 'Y-m-01 00:00:00' ); $last_day = date( 'Y-m-t 23:59:59' ); $meta_query = array( array( 'key' => '_EventStartDate', 'value' => array( $first_day, $last_day ), 'compare' => 'BETWEEN', 'type' => 'DATETIME', ) ); return array_merge( $query_args, array( 'meta_key' => '_EventStartDate', 'meta_type' => 'DATETIME', 'orderby' => 'meta_value', 'order' => 'ASC', 'meta_query' => $meta_query, ) ); } return $query_args; }, 10, 2 );
// Event list from next month add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'next-month-events' ) !== false ) { $next_month = date( 'Y-m-d H:i:s', strtotime( 'first day of next month' ) ); $meta_query = array( array( 'key' => '_EventStartDate', 'value' => $next_month, 'compare' => '>=', 'type' => 'DATETIME', ) ); return array_merge( $query_args, array( 'meta_key' => '_EventStartDate', 'meta_type' => 'DATETIME', 'orderby' => 'meta_value', 'order' => 'ASC', 'meta_query' => $meta_query, ) ); } return $query_args; }, 10, 2 );
-
Ah good job 🙂
Glad it works now!
- You must be logged in to reply to this topic.