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 (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

GenerateBlocks Query Loop Filter by Custom Field

  • Hi there,
    I have a custom post type called Events, and each Single Event has a custom field called event_date. How can I use a query loop to display a list of events, excluding those events where event_date < current date (i.e., the event has already passed)? Thank you!

  • Hi there,

    it would require some PHP to write your own query args using the GB generateblocks_query_loop_args hook.
    See here fore some examples:

    For your specific needs you would do something like this:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        // apply filter if loop has class: order-by-priority
        if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'current-events' ) !== false ) {
            $new_query_args = array(
                'meta_query' => array(
                        'key' => 'event_date', // Assuming 'event_date' is your custom field name
                        'value' => date( 'Y-m-d' ), 
                        'compare' => '>=', 
                        'type' => 'DATE',
            // Merge new query arguments with existing ones
            $query_args = array_merge( $query_args, $new_query_args );
        return $query_args;
    }, 10, 2 );

    Note here: 'current-events' ) !== false ) is where i set the class of current-events which is the class you use in Step 1 of the aboce doc.

    And here: 'key' => 'event_date', // Assuming 'event_date' is your custom field name you define the custom field name.

  • I did everything as you suggested, but now the query loop is empty. I thought the issue might be with the output format of the custom field “event_date”, so I tried changing it to “Y-m-d”, but it didn’t help. I would appreciate it if you could take a look at the site. Thank you.

  • Ah damnit… i updated the code above ( Where i forgot to merge the args back in ).
    Give that a try

  • Perfect, thank you so much!

  • You’re welcome

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