WordPress 5.8 Query Loop block: a ready-made “Events” variation
WordPress 5.8 added the Query Loop block: a list of posts with a query chosen in the block settings (post type, categories, order, number) and a layout built from blocks such as Post Title, Post Excerpt and Post Featured Image. Editors can build post lists without a custom block. For a theme or plugin, a block variation turns a common configuration into one click: an “Events list” in the inserter that is already set to the right post type and layout.
Problem: Editors build the same “upcoming events” list again and again from a Query Loop block: choose the event post type, add title, date and excerpt, set the number of posts.
Solution: Register a variation of core/query with wp.blocks.registerBlockVariation(), with preset query attributes and inner blocks, and load the script on enqueue_block_editor_assets.
The variation
js/query-variations.js, plain JavaScript without a build step:
wp.domReady( function () {
wp.blocks.registerBlockVariation( 'core/query', {
name: 'ha-events-list',
title: 'Events list',
description: 'The next events, newest first.',
icon: 'calendar-alt',
scope: [ 'inserter' ],
attributes: {
query: {
postType: 'event',
perPage: 6,
pages: 0,
offset: 0,
order: 'desc',
orderBy: 'date',
inherit: false
}
},
innerBlocks: [
[ 'core/post-template', {}, [
[ 'core/post-title', { isLink: true } ],
[ 'core/post-date' ],
[ 'core/post-excerpt' ]
] ],
[ 'core/query-pagination' ]
]
} );
} );
<?php
add_action( 'enqueue_block_editor_assets', function () {
wp_enqueue_script(
'ha-query-variations',
get_template_directory_uri() . '/js/query-variations.js',
array( 'wp-blocks', 'wp-dom-ready' ),
filemtime( get_template_directory() . '/js/query-variations.js' ),
true
);
} );
The event post type must be registered with 'show_in_rest' => true; the Query Loop block lists only post types that are available in the REST API. inherit: false means the block runs its own query instead of taking the main query of the page, which is what a list on the home page or a landing page needs.
After inserting, the editor can still change everything: the number of posts, the layout, the filters. The variation is only a starting point.
What 5.8 cannot do yet
The block’s query options are limited to what the settings panel offers: post type, categories, tags, author, keyword, order by date or title, sticky posts. Ordering by a custom field, such as the event date, or showing only future events is not possible in 5.8 without a custom block. Until then, a simple solution is to keep the event date as the post date (scheduled posts are not shown, so publish events with their real date and list them by date).
Update (2022): WordPress 6.1 added the query_loop_block_query_vars filter, which lets PHP change the query of a Query Loop block before it runs, for example to order events by a meta field and hide past ones. Combined with a namespace attribute in the variation, the filter can target only your own variation.
NOTE: The Query Loop block renders on the server with the same template parts as the editor preview, so its output follows the theme’s styles for the inner blocks. Style the variation through classes on the blocks (the “Additional CSS class” field) rather than by targeting the generic .wp-block-query, which is used by every Query Loop on the site.