-
Hi,
I have a cpt ‘courses’ with a datefield (startdate).
I have a query loop block to display the courses ordered by the date field.
Is it possible to show only the actual courses, so after today?
Thanks in advance,
Sandra
-
Alvind
Hi there,
You can try this snippet:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'show-future-courses' ) !== false ) { // Get the current date $current_date = date('Y-m-d'); // Modify query arguments $query_args = array_merge( $query_args, array( 'meta_key' => 'course_date', 'meta_type' => 'DATE', 'orderby' => 'meta_value', 'order' => 'ASC', 'meta_query' => array( array( 'key' => 'course_date', 'value' => $current_date, 'compare' => '>', 'type' => 'DATE' ), ), )); } return $query_args; }, 10, 2 );
Replace the
course_date
meta key to the actual date field name.Adding PHP: https://docs.generatepress.com/article/adding-php/
Then, add the class
show-future-courses
to the Grid block inside the Query Loop block in the Additional CSS Classes field.
https://docs.generateblocks.com/article/order-query-loop-by-custom-field/#1-add-an-additional-css-class-to-the-query-loop -
It works!
Thanks for the rapid response!
Regards, Sandra
-
Alvind
No problem! 😁
Viewing 4 posts - 1 through 4 (of 4 total)
- You must be logged in to reply to this topic.