From 362bc1660db49236a32b0c9d199bab41cef1ef13 Mon Sep 17 00:00:00 2001 From: Maud Royer Date: Thu, 5 Feb 2026 19:50:11 +0100 Subject: [PATCH 1/5] Blocks: Add helper functions for Terms Query pagination. Add `build_terms_query_vars_from_block()` and `terms_query_register_query_vars()` to support pagination in the Terms Query block. These functions are used by the new Terms Query Pagination blocks (core/terms-query-pagination, core/terms-query-pagination-next, core/terms-query-pagination-previous, core/terms-query-pagination-numbers) and the updated Term Template block. --- src/wp-includes/blocks.php | 71 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 89007d0d0d036..56cb571a4b9ed 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -3049,6 +3049,77 @@ function get_comments_pagination_arrow( $block, $pagination_type = 'next' ) { return null; } +/** + * Builds query vars for terms from a Terms Query block instance. + * + * It's used with the Terms Query inner blocks (Term Template, Terms Query Pagination). + * + * @since 7.0.0 + * + * @param WP_Block $block Block instance. + * @return array Query vars suitable for get_terms() or wp_count_terms(). + */ +function build_terms_query_vars_from_block( $block ) { + $query = $block->context['termQuery']; + + $query_vars = array( + 'number' => $query['perPage'], + 'order' => $query['order'], + 'orderby' => $query['orderBy'], + 'hide_empty' => $query['hideEmpty'], + ); + + $inherit_query = isset( $query['inherit'] ) && $query['inherit'] && ( is_tax() || is_category() || is_tag() ); + + if ( $inherit_query ) { + // Get the current term and taxonomy from the queried object. + $queried_object = get_queried_object(); + + // For hierarchical taxonomies, show children of the current term. + // For non-hierarchical taxonomies, show all terms (don't set parent). + if ( is_taxonomy_hierarchical( $queried_object->taxonomy ) ) { + // If showNested is true, use child_of to include nested terms. + // Otherwise, use parent to show only direct children. + if ( ! empty( $query['showNested'] ) ) { + $query_vars['child_of'] = $queried_object->term_id; + } else { + $query_vars['parent'] = $queried_object->term_id; + } + } + $query_vars['taxonomy'] = $queried_object->taxonomy; + } else { + // If not inheriting set `taxonomy` from the block attribute. + $query_vars['taxonomy'] = $query['taxonomy']; + + // If we are including specific terms we ignore `showNested` argument. + if ( ! empty( $query['include'] ) ) { + $query_vars['include'] = array_unique( array_map( 'intval', $query['include'] ) ); + $query_vars['orderby'] = 'include'; + $query_vars['order'] = 'asc'; + } elseif ( is_taxonomy_hierarchical( $query['taxonomy'] ) && empty( $query['showNested'] ) ) { + // We set parent only when inheriting from the taxonomy archive context or not + // showing nested terms, otherwise nested terms are not displayed. + $query_vars['parent'] = 0; + } + } + + return $query_vars; +} + +/** + * Registers the 'termspage' query variable for terms pagination. + * + * @since 7.0.0 + * + * @param array $vars The array of existing query variables. + * @return array Modified query variables including 'termspage'. + */ +function terms_query_register_query_vars( $vars ) { + $vars[] = 'termspage'; + return $vars; +} +add_filter( 'query_vars', 'terms_query_register_query_vars' ); + /** * Strips all HTML from the content of footnotes, and sanitizes the ID. * From 0267d237650e5809e9718b9a55213ed718196d86 Mon Sep 17 00:00:00 2001 From: Maud Royer Date: Fri, 6 Feb 2026 13:21:36 +0100 Subject: [PATCH 2/5] Blocks: Improve comments type annotations for Terms Query helper functions. --- src/wp-includes/blocks.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 56cb571a4b9ed..f3d994280f8fd 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -3057,7 +3057,7 @@ function get_comments_pagination_arrow( $block, $pagination_type = 'next' ) { * @since 7.0.0 * * @param WP_Block $block Block instance. - * @return array Query vars suitable for get_terms() or wp_count_terms(). + * @return array Query vars suitable for get_terms() or wp_count_terms(). */ function build_terms_query_vars_from_block( $block ) { $query = $block->context['termQuery']; @@ -3107,12 +3107,12 @@ function build_terms_query_vars_from_block( $block ) { } /** - * Registers the 'termspage' query variable for terms pagination. + * Registers the 'termspage' public query variable to support pagination for the Terms Query block. * * @since 7.0.0 * - * @param array $vars The array of existing query variables. - * @return array Modified query variables including 'termspage'. + * @param array $vars The array of existing query variables. + * @return array Modified query variables including 'termspage'. */ function terms_query_register_query_vars( $vars ) { $vars[] = 'termspage'; From f51ed15fa90ebf817eff776f2a7f94b6fe1fd8a8 Mon Sep 17 00:00:00 2001 From: Maud Royer Date: Sun, 8 Feb 2026 13:41:33 +0100 Subject: [PATCH 3/5] Blocks: Register 'termspage' query var in WP::$public_query_vars. Move 'termspage' from a `query_vars` filter in blocks.php to the `WP::$public_query_vars` default array in class-wp.php. --- src/wp-includes/blocks.php | 14 -------------- src/wp-includes/class-wp.php | 2 +- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index f3d994280f8fd..5c4618d03cd84 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -3106,20 +3106,6 @@ function build_terms_query_vars_from_block( $block ) { return $query_vars; } -/** - * Registers the 'termspage' public query variable to support pagination for the Terms Query block. - * - * @since 7.0.0 - * - * @param array $vars The array of existing query variables. - * @return array Modified query variables including 'termspage'. - */ -function terms_query_register_query_vars( $vars ) { - $vars[] = 'termspage'; - return $vars; -} -add_filter( 'query_vars', 'terms_query_register_query_vars' ); - /** * Strips all HTML from the content of footnotes, and sanitizes the ID. * diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index f1664747d4042..0b3765246b4b0 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -15,7 +15,7 @@ class WP { * @since 2.0.0 * @var string[] */ - public $public_query_vars = array( 'm', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'pagename', 'page_id', 'error', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'favicon', 'taxonomy', 'term', 'cpage', 'post_type', 'embed' ); + public $public_query_vars = array( 'm', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'pagename', 'page_id', 'error', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'favicon', 'taxonomy', 'term', 'cpage', 'post_type', 'embed', 'termspage' ); /** * Private query variables. From ee7cb465515f4d9bc2b8f5485068126451537e5e Mon Sep 17 00:00:00 2001 From: Maud Royer Date: Sun, 8 Feb 2026 22:29:12 +0100 Subject: [PATCH 4/5] Tests: Add 'termspage' to expected public query vars. --- tests/phpunit/tests/query/vars.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/tests/query/vars.php b/tests/phpunit/tests/query/vars.php index 87e7fa7bb75c6..886fdf4370f54 100644 --- a/tests/phpunit/tests/query/vars.php +++ b/tests/phpunit/tests/query/vars.php @@ -67,6 +67,7 @@ public function testPublicQueryVarsAreAsExpected() { 'cpage', 'post_type', 'embed', + 'termspage', // Dynamically added public query vars: 'post_format', From c83882de166b93cf9f41d11b6452a357ddf7ddbf Mon Sep 17 00:00:00 2001 From: Maud Royer Date: Tue, 24 Feb 2026 10:24:04 +0100 Subject: [PATCH 5/5] Add types declarations to `build_terms_query_vars_from_block` Co-authored-by: Weston Ruter --- src/wp-includes/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 5c4618d03cd84..5f29b666b3626 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -3059,7 +3059,7 @@ function get_comments_pagination_arrow( $block, $pagination_type = 'next' ) { * @param WP_Block $block Block instance. * @return array Query vars suitable for get_terms() or wp_count_terms(). */ -function build_terms_query_vars_from_block( $block ) { +function build_terms_query_vars_from_block( WP_Block $block ): array { $query = $block->context['termQuery']; $query_vars = array(