-
I have a block that is a child of Query Loop block (#43449). This block uses the query context. Parent block (posts-section)import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';
import { BlockEditProps, TemplateArray } from '@wordpress/blocks';
export default () => {
const blockProps = useBlockProps();
let TEMPLATE: TemplateArray = [
['my/section-header'],
['core/query', {}, [['my/posts-template']]],
];
const innerBlockProps = useInnerBlocksProps(blockProps, {
template: TEMPLATE,
templateLock: 'all',
});
return <section {...innerBlockProps} />;
}; Block.json{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "my/posts-template",
"title": "Posts template",
"category": "text",
"parent": ["my/posts-section", "core/query"],
"usesContext": ["queryId", "query", "queryContext"],
"supports": {
"html": false
},
"editorScript": "file:./index.ts",
"editorStyle": "file:./index.css",
"style": "file:./style-index.css"
} JSimport { useBlockProps } from '@wordpress/block-editor';
import ServerSideRender from '@wordpress/server-side-render';
export default ({ context }) => {
const blockProps = useBlockProps();
return (
<div {...blockProps}>
<ServerSideRender block="my/posts-template" />
</div>
);
}; PHPpublic function render_callback( array $attributes, string $content, WP_Block $block ): string {
$query_args = build_query_vars_from_query_block( $block, 1 );
$query = new WP_Query( $query_args );
if ( ! $query->have_posts() ) {
return '';
}
$content = '';
while ( $query->have_posts() ) {
$query->the_post();
ob_start(); ?>
<article>
<!--Test-->
<?php the_title() ?>
</article>
<?php $content .= ob_get_clean();
}
wp_reset_postdata();
return "<div>$content</div>";
} The Is there a workaround for this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Right after posting this question I came up with a workaround. export default ({ context }) => {
const blockProps = useBlockProps();
return (
<div {...blockProps}>
<ServerSideRender block="my/posts-template" attributes={context} />
</div>
);
}; public function render_callback( array $attributes, string $content, WP_Block $block ): string {
if ( empty( $block->context ) ) {
$block->context = $attributes;
}
$query_args = build_query_vars_from_query_block( $block, 1 );
$query = new WP_Query( $query_args );
// Rest of the code
} Block.json: "attributes": {
"queryId": {
"type": "number"
},
"query": {
"type": "object"
}
}, |
Beta Was this translation helpful? Give feedback.
-
This is a known issue. It's being tracked in #40714. It'd be great to fix it directly in the block editor to pass the context correctly. |
Beta Was this translation helpful? Give feedback.
This is a known issue. It's being tracked in #40714. It'd be great to fix it directly in the block editor to pass the context correctly.