diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 89007d0d0d036..5f29b666b3626 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -3049,6 +3049,63 @@ 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( WP_Block $block ): array { + $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; +} + /** * 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. 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',