I’m encountering an issue with a custom block variation of the core/query block in Gutenberg. Specifically, I’m missing the Grid View option in my custom block variation.
Here’s my variation registration code:
const VARIATION_NAME = 'custom_variation_query_loop';
registerBlockVariation('core/query', {
name: custom_variation_query_loop,
title: 'Custom Variation Query Loop',
description: 'Variation (Custom)',
usesContext: ["query", "displayLayout"],
isActive: ( blockAttributes ) =>
blockAttributes.namespace === VARIATION_NAME,
icon: 'admin-links',
attributes: {
namespace: VARIATION_NAME,
displayLayout: {
type: 'flex',
columns: 2,
},
query: {
postType: 'destination',
orderBy: 'title',
perPage: 12,
offset: 0,
},
},
innerBlocks: [
[
'hwrdp/post-template-id-filter',
{},
[
['core/post-title', { level: 0, isLink: true, fontSize: 'large' }],
],
],
['core/query-no-results'],
],
scope: [ 'block', 'inserter' ],
});
Everything else in the custom variation works as expected. However, the Grid View option just isn’t available.
Has anyone encountered a similar issue? Any pointers on what might be causing this and how to fix it?
- Registered a custom block variation for core/query using registerBlockVariation.
- Provided necessary attributes including the displayLayout with a flex layout and 2 columns.
- Checked and ensured that the theme, WordPress, and all related plugins are updated.
- Verified the PHP side to make sure there’s no interference, and it seems unrelated to the problem
2
Answers
If anyone else encounters a similar issue, here's the solution I found:
The Grid/Post view wasn't displaying as expected. Upon investigation, it was revealed that the editor did not display posts within the query loop, which was due to the use of a custom post type and the modified query.
To address this:
rest_{$this->post_type}
_query filter. This ensures the posts are displayed correctly within the editor when using a custom post type.Additionally, a big shoutout to @S.Walsh for pointing out naming and recommending the explicit mention of layout: grid. Appreciate the insight
I tested your variation code and the grid option appears as expected when you select the Post Template of your Custom Variation Query Loop:
I would try using the default post template to rule out any potential issues with the custom
[hwrdp/post-template-id-filter]
block, eg:Other than that, the only issue I can see is a potential typo for the value of
name
, I would expect it to be: