I’m trying to apply the pre_get_posts
to a specific core/query block, in order to programatically modify the number of posts displayed when a custom attribute is set. If I use the render_block
hook, the block is already pre-rendered and so modifying the query using pre_get_posts
has no effect. Is there any way to achieve this?
(I don’t need help adding the custom attribute; I’ve done so already using block filters.)
A modified version of the suggestion from @birgire works. Note that this applies to the core/post-template
block, not the core/query block
.
add_filter('block_type_metadata_settings', function ($settings, $metadata) {
if ($metadata['name'] !== 'core/post-template') {
return $settings;
}
if ($settings['render_callback'] !== 'render_block_core_post_template') {
return $settings;
}
$settings['render_callback'] = 'wpse_render_block_core_post_template';
return $settings;
}, 10, 2);
function wpse_render_block_core_post_template($attributes, $content, $block)
{
$callback = fn ($query) => $query->set('posts_per_page', 2);
add_action('pre_get_posts', $callback, 999);
$output = render_block_core_post_template($attributes, $content, $block);
remove_action('pre_get_posts', $callback, 999);
return $output;
}