Customising the query loop block order
-
I’m using the Query loop block to load our custom post type ‘event’ on the events archive template page, inherit query for template is unchecked.
What we’d like to do is override the default order set and order by the custom ACF field ‘start_date’.
I’ve seen some Javascript solutions that suggest you can create variations but wondered if it’s possible to override the output by creating a function instead? Something like the code below, just wanting to explore all possibilities before going down the variation route.
function override_event_block_query( $query ) { // Check if this is the main query and if we are on the 'event' post type archive if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'event' ) ) { // Modify the query to order by the 'start_date' ACF field $query->set( 'meta_key', 'start_date' ); $query->set( 'orderby', 'meta_value' ); $query->set( 'order', 'ASC' ); } } add_action( 'pre_get_posts', 'override_event_block_query' );
We are using a 2024 block child theme
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Customising the query loop block order’ is closed to new replies.