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;
}
Related Posts:
- When to use WP_query(), query_posts() and pre_get_posts
- Using pre_get_posts with WP_Query
- WP_Query orderby post__in remains ineffective in the Loop [closed]
- Reduce or prevent calling of update_meta_cache
- How to add taxonomy filter on the query fly?
- Order Search Results Page by meta_value If no Value Return Remaining Results
- How-to exclude terms from the main query the most performant way?
- Changing Posts Per Page and offset with pre_get_posts
- Slow SQL_CALC_FOUND_ROWS Query
- “pre_get_posts” firing on every query
- RSS feed with specific keyword
- Using is_main_query to select custom post type on certain page
- SQL Statement generated by WP_Query not producing expected results
- Sorting Posts by custom field
- How to pass custom parameter to WP_Query for filtering in pre_get_posts
- Recommended way to drop a pending query (in pre_get_posts)?
- WP Query by Gutenberg block and get its attribute
- WordPress custom search form with pre_get_posts not work
- If orderby parameter using pre_get_posts is the same for multiple posts what fallback does the query use?
- Can not switch the queried post in pre_get_posts hook
- How to target the default Recent Posts and Recent Comments widgets with pre_get_posts?
- order by multiple meta keys in pre_get_posts
- How to sort posts in admin by titles with dd.mm.yyyy format?
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- How to Modify Taxonomy Archive Page with Search Parameter?
- Searching through different categories on different pages code is not working
- How to display future posts – modified query still yields 404
- How to query for custom field within Gutenberg block from outside of that post? [closed]
- pre_get_posts filter meta_query without conflicting existing meta_query
- Including only current user’s posts in search
- When querying a combination of posts and other meta fields, is there a better solution than directly modifying the WHERE value?
- How to change posts order on Category and Tag archives?
- My Main Query Modification is Messing up my dynamic main – why?
- pre_get_posts Remove tax_query Completely
- Single meta_query query using OR instead of AND in request’s WHERE statement
- How to choose between hooking into per_get_posts or into parse_query
- Creating query to show which editor (classic or block) was last used to edit a post/page
- Store sticky post’s ids in a transient
- new WP_Query to get max price meta value not working
- Show Sticky Post at the top but do not show again in the loop?
- WP_Query, pre_get_posts and offset
- Using meta_query with multiple keys and compare values
- Can I set my WP_Query to be a Main Query?
- Remove category from query (show all posts in archive.php) pre_get_posts()
- Show scheduled posts in main loop but not in WP_Query?
- hide custom post types with specific meta key ON admin backend
- WP_Query ignoring tax_query when is_singular
- Applying posts_clauses filter to specific queries only
- Help ordering Post loop by two meta values
- Order ascending is ignored in meta query?
- New WP_Query calls Pre_get_posts filter twice
- Shouldn’t I be able to modify the main query by this filter?
- How Can I Change The Tax Query For The Main Loop For Taxonomy Archives?
- using pre_get_posts for search results not found
- Hide elements outside loop based on query
- Woocommerce – exclude only older out-of-stock items
- WP Query – order posts by meta field first and then order the rest
- queried_object using pre_get_posts gets notices and warning
- date_query in pre_get_posts out of memory
- How I can change the condition or compare operator for WP_Query in pre_get_posts
- WP_Query – How to get all posts of specific days of week by custom field date?
- Sorting search results with custom dropdown
- pre_get_posts – editing query, tax_query
- tax_query in pre_get_posts
- $wp_query->found_posts not returning correct value
- WP_Query, Paginate by Custom Field
- Elementor posts custom query based on tag [closed]
- Exclude post with taxonomy and from the terms (taxonomy & tag) posts count
- `offset` WP_Query argument dont work via `pre_get_posts`
- Replace wp_query with wp_user_query
- Block internal search queries with pre_get_posts and regex rules
- Make a products only viewable to a specific user ID in meta_query pre_get_posts
- is_user_logged_in not working from WP-API (wp-json)
- Search Exact on WordPress – Relevannsi Solution Not Working
- pre_get_posts pagination not working
- meta_query is overriding default search
- order posts by a secondary query that counts items
- How can I re-query post_type and rewrite the url?
- Pre get posts sort by meta key returns no results if meta key does not exist
- Pre_get_posts only show posts by administrator roles
- Modify a query with no results in pre_get_posts
- Woocomerce – Order products by float attribute in archive pages
- Order posts by title and custom field value?
- Custom Order Current Query: By Meta Key and Category
- Limit default Search query to post_title
- why doesn’t this pre_get_posts code work?
- pre_get_posts with WP_Query to prevent posts from specific tags
- Using the same WP_Query for shop and widgets in WooCommerce shop
- Modify query to exclude certain ids and certain parents and its corresponding children
- Adding a Theme Customizer Control for Posts_Per_Page
- Order posts by custom column using pre_get_posts
- Sort posts in dashboard using custom field; also include posts where field isn’t set
- Looking for a way to exclude frontpage and nav menu from query filter
- pre_get_posts redirecting
- Custom post types loop on a page template
- Search query alteration not working for meta values
- How to put posts with some taxonomy on top of others in `pre_get_posts`
- Block Editor – WordPress 6.1 – CPT Archive Issue – While the title changes in the loop, all records display the same data
- When to use WP_query(), query_posts() and pre_get_posts
- query loop “inherit query from template” prevents setting sort order