I am attempting to add some context to the Query Loop block and inherit that on the inner blocks like post title (so nested inside the Post Template block). I can filter it in on render_block_context and the context is available on the Query Loop block, but I was hoping to figure out how to automatically inherit that in the descendant blocks.
Here's my minimal example:
add_filter( 'render_block_context', function( $context, $parsed_block ) {
if ( 'core/query' === $parsed_block['blockName'] ) {
$context['helga/tacos'] = 'we love tacos';
}
return $context;
}, 10, 2 );
add_filter( 'block_type_metadata', function( $metadata ) {
if ( 'core/query' === $metadata['name'] ) {
$metadata['attributes'] ??= [];
$metadata['attributes']['tacos'] = [
'type' => 'string',
'default' => 'default tacos',
];
$metadata['providesContext'] ??= [];
$metadata['providesContext']['helga/tacos'] = 'tacos';
}
if ( 'core/post-title' === $metadata['name'] ) {
$metadata['usesContext'] ??= [];
$metadata['usesContext'][] = 'helga/tacos';
}
return $metadata;
} );
add_filter( 'render_block_core/post-title', function( $block_content, $block, $instance) {
return $block_content . json_encode( $instance->context );
}, 10, 3 );
Then insert a Query Loop block anywhere and look at the resulting output. I have the context json encoded after each post title... and it never includes the helga/tacos context key that I defined both in render_block_context but also set up providesContext and usesContext for in the loop and title blocks respectively.
I am trying to get the helga/tacos key available in the context in the post title block AND for it's value to be we love tacos.