Skip to content
1 change: 1 addition & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Enhancements

- Term Count: Reuse filtered terms from Term Template so changes made through the `get_terms` filter appear on the front end without a separate query for each count ([#84119](https://github.com/WordPress/gutenberg/pull/84119)).
- Query: List the existing terms in the taxonomy filters, so categories and tags can be browsed and selected instead of recalled and typed. The controls now use `SearchableChipSelectControl` from `@wordpress/ui` ([#82583](https://github.com/WordPress/gutenberg/pull/82583)).
- Icon: Speed up loading icons in the icon library ([#84327](https://github.com/WordPress/gutenberg/pull/84327)).
- Video: Don't autoplay on the front end when the visitor prefers reduced motion ([#84308](https://github.com/WordPress/gutenberg/pull/84308)).
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/term-count/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ _Defined via the [`usesContext` and `providesContext`](https://developer.wordpre

- `termId`
- `taxonomy`
- `termData`

## Block Markup

Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/term-count/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"category": "theme",
"description": "Displays the post count of a taxonomy term.",
"textdomain": "default",
"usesContext": [ "termId", "taxonomy" ],
"usesContext": [ "termId", "taxonomy", "termData" ],
"attributes": {
"bracketType": {
"type": "string",
Expand Down
6 changes: 4 additions & 2 deletions packages/block-library/src/term-count/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
* @return string Returns the count of the current taxonomy term wrapped inside a heading tag.
*/
function render_block_core_term_count( $attributes, $content, $block ) {
// Get term from context or from the current query.
if ( isset( $block->context['termId'] ) && isset( $block->context['taxonomy'] ) ) {
// Reuse the filtered term from Term Template to avoid a separate query for each count.
if ( isset( $block->context['termData'] ) && $block->context['termData'] instanceof WP_Term ) {
$term = $block->context['termData'];
} elseif ( isset( $block->context['termId'] ) && isset( $block->context['taxonomy'] ) ) {
$term = get_term( $block->context['termId'], $block->context['taxonomy'] );
} else {
$term = get_queried_object();
Expand Down
6 changes: 3 additions & 3 deletions packages/block-library/src/term-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ function render_block_core_term_template( $attributes, $content, $block ) {
}
}

$terms_query = new WP_Term_Query( $query_args );
$terms = $terms_query->get_terms();
$terms = get_terms( $query_args );

if ( ! $terms || is_wp_error( $terms ) ) {
return '';
Expand All @@ -85,9 +84,10 @@ function render_block_core_term_template( $attributes, $content, $block ) {
$term_id = $term->term_id;
$taxonomy = $term->taxonomy;

$filter_block_context = static function ( $context ) use ( $term_id, $taxonomy ) {
$filter_block_context = static function ( $context ) use ( $term_id, $taxonomy, $term ) {
$context['termId'] = $term_id;
$context['taxonomy'] = $taxonomy;
$context['termData'] = $term;
return $context;
};

Expand Down
Loading