• dreamabstract

    (@dreamabstract)


    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)
  • Moderator jordesign

    (@jordesign)

    Hey @dreamabstract – I think you’re probably on the right track there. I’d expect using pre_get_posts to be the right way to adjust that query – and then keeping ‘Inherit query’ switched on should use it.

    I’ll admit I’m not certain if there’s anything specific to ACF that would need to be different though. I’d recommend also asking this question in the forums for ACF, or the general WordPress forums as well (as this isn’t specific to the Twenty Twenty-Four theme).

Viewing 1 replies (of 1 total)
  • The topic ‘Customising the query loop block order’ is closed to new replies.