Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2973,6 +2973,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<string, mixed> 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'],
Comment on lines +2990 to +2993
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these keys absolutely guaranteed to exist? I see below an isset() check for yet another key. If not, should defaults be provided? Or should they populate $query_vars only when set?

Copy link
Author

@jillro jillro Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, those keys are guaranteed to exist.

This code is just former Terms Query Template block code factorized to be used also in pagination, but apart from that, I left it untouched.

);

$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.
*
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/query/vars.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function testPublicQueryVarsAreAsExpected() {
'cpage',
'post_type',
'embed',
'termspage',

// Dynamically added public query vars:
'post_format',
Expand Down
Loading