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.

Creating an “Upcoming Events” page with Query Loops: Date handling

  • Hello guys,

    I saw this post: https://generatepress.com/forums/topic/creating-an-upcoming-events-page-with-query-loops-date-handling/.

    I have also got into the same situation. I have made a custom post type for my events. I want to show the events that are today or in the future after today. I saw ou guys are adding this in the future. I found the possibility to do it after today. I think this is the date that the activity is made and not the date i added with the ACF plugin. Also the date is not ‘after today’ but a static date.

    Did you guys add something like this so i can show only the post that are after today? In a custom field where i have the date?

    Or else do you guys know a workaround? If you have any questions please let me know.

    Have a great day 😀

  • Hi there,

    today, it requires some custom query args to be passed to the query loop.
    The following PHP Snippet would do that.

    
    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    
        // Apply filter if loop has class: custom-event-query
        if ( 
            ! is_admin() && 
            ! empty( $attributes['className'] ) && 
            strpos( $attributes['className'], 'custom-event-query' ) !== false 
        ) {
           
            // Add meta_query to filter posts with meta value (date) greater than or equal to current date
            $query_args['meta_query'] = array(
                array(
                    'key' => 'my-custom-date',
                    'value' => date( 'Y-m-d' ), // Current date in 'YYYY-MM-DD' format
                    'compare' => '>=',
                    'type' => 'DATE',
                ),
            );
    
            // Merge existing query_args with additional args for ordering
            $query_args = array_merge( $query_args, array(
                'meta_key' => 'my-custom-date',
                'orderby' => 'meta_value',
                'order' => 'ASC',
            ));
        }
        return $query_args;
    }, 10, 2 );
    

    Notes:
    strpos( $attributes['className'], 'custom-event-query' ) !== false here the custom-event-query is the CSS class that you add to the Query Loops > Grid Blocks > Advanced > Additional CSS Classes field.

    'key' => 'my-custom-date', and 'meta_key' => 'my-custom-date', we set the custom field name of my-custom-date change that to the name of your custom field that holds the event date you want to filter by.

    Those args will list only posts on or after the my-custom-date and in that date order.

    The rest of your query parameters such as the Post Type and number of posts to display should be added to the block settings.

  • Hey @david,

    Excellent this is exactly what i was looking for. Thanks for the information and the quick response!

    Have a great day!

  • Awesome – glad to hear that!

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