apply_filters( 'query_loop_block_query_vars', array $query, WP_Block $block, int $page )

Filters the arguments which will be passed to WP_Query for the Query Loop Block.


Description

Anything to this filter should be compatible with the WP_Query API to form the query context which will be passed down to the Query Loop Block’s children.
This can help, for example, to include additional settings or meta queries not directly supported by the core Query Loop Block, and extend its capabilities.

Please note that this will only influence the query that will be rendered on the front-end. The editor preview is not affected by this filter. Also, worth noting that the editor preview uses the REST API, so, ideally, one should aim to provide attributes which are also compatible with the REST API, in order to be able to implement identical queries on both sides.


Top ↑

Parameters

$query array
Array containing parameters for WP_Query as parsed by the block context.
$block WP_Block
Block instance.
$page int
Current query's page.

Top ↑

Source

File: wp-includes/blocks.php. View all references

return apply_filters( 'query_loop_block_query_vars', $query, $block, $page );


Top ↑

Changelog

Changelog
Version Description
6.1.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by nicktrosnant

    Here is an example of handing a custom post type which holds an event date field in metadata and where I wanted the Query Loop to display posts sorted by the event date rather than the post date. I also wanted to exclude dates in the past.

    add_filter( 'query_loop_block_query_vars', 'wpdocs_filter_query' );
    function wpdocs_filter_query( $query ) {
        // ignore if the query block is not using this post type
        if ( 'wpdocs_event' !== $query['post_type'] ) {
            return $query;
        }
    
        // always exclude events with dates in the past
        $query['meta_key'] = 'eventDate';
        $query['meta_value'] = date( 'Y-m-d' );
        $query['meta_compare'] = '>=';
    
        // If date order was chosen in the block settings, change to use the Event date instead of Post date
        if ( 'date' === $query['orderby'] ) {
            $query['orderby'] = 'meta_value';
        }
    
        return $query;
    }

You must log in before being able to contribute a note or feedback.