Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Group query loop per month by post meta date field

  • 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/

  • 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 🙁

  • 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!

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.