Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c18d62c
nesting & inheritance features split out from #71596
cr0ybot Sep 25, 2025
3b7be0e
rename build_query_vars_from_terms_query_block function
cr0ybot Sep 26, 2025
344e866
guard against redeclaration
cr0ybot Sep 26, 2025
569845c
move getQueryContextFromTemplate to shared location, provide template…
cr0ybot Sep 26, 2025
56c09e7
simplify inheritance to be driven only by terms query block, strip ta…
cr0ybot Sep 26, 2025
4eaccdd
move and update getQueryContextFromTemplate tests
cr0ybot Sep 26, 2025
58d1346
apply classes similar to post-template block for taxonomy
cr0ybot Sep 26, 2025
b0cc2e8
move experimental terms query features to load conditionally
cr0ybot Sep 27, 2025
d70fa73
fix phpcs warning
cr0ybot Sep 27, 2025
5879905
remove backport changelog until block is no longer experimental
cr0ybot Sep 28, 2025
00b3ec1
cleanup
cr0ybot Oct 2, 2025
9e38dd5
explicitly specify hierarchical = false in term queries
cr0ybot Oct 2, 2025
3369fd0
taxonomy should be inherited only if there is a termId in context to …
cr0ybot Oct 2, 2025
1cb5726
fix hierarchical terms displayed for post context
cr0ybot Oct 2, 2025
bfa9df9
fix nested terms inherited from a post, but breaks nested terms that …
cr0ybot Oct 3, 2025
cc4f0fd
fix nested term query context from post or term archive
cr0ybot Oct 3, 2025
94f3390
fix getting taxonomy/term from template slug with special handling fr…
cr0ybot Oct 3, 2025
1f54b24
fix taxonomy selector while inherit is true, breaks auto-inherit from…
cr0ybot Oct 4, 2025
6f23abe
const variable assignment
cr0ybot Oct 6, 2025
fe582e7
remove explicit default context
cr0ybot Oct 6, 2025
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
3 changes: 1 addition & 2 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,6 @@ Contains the block elements used to render a taxonomy term, like the name, descr
- **Category:** theme
- **Ancestor:** core/terms-query
- **Supports:** align (full, wide), color (background, gradients, link, text), interactivity (clientNavigation), layout (allowEditing, allowSwitching, default), spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~, ~~reusable~~
- **Attributes:** namespace

## Terms Query

Expand All @@ -1007,7 +1006,7 @@ An advanced block that allows displaying taxonomy terms based on different query
- **Category:** theme
- **Allowed Blocks:** core/term-template
- **Supports:** align (full, wide), interactivity, ~~html~~
- **Attributes:** namespace, tagName, termQuery, termQueryId, termsToShow
- **Attributes:** namespace, tagName, termQuery, termQueryId

## Text Columns (deprecated)

Expand Down
108 changes: 108 additions & 0 deletions lib/compat/wordpress-6.9/terms-query-block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Terms Query block helpers.
*
* @since 6.9.0
* @package gutenberg
* @subpackage Blocks
*/

if ( ! function_exists( 'build_query_vars_from_terms_query_block' ) ) {
/**
* Helper function that constructs a WP_Term_Query args array from a Terms Query
* block's attributes.
*
* @param WP_Block $block Block instance.
* @param int $page Current query's page.
*
* @return array Returns the constructed WP_Term_Query arguments.
*/
function build_query_vars_from_terms_query_block( $block, $page = 1 ) {
$query = array(
'taxonomy' => 'category',
'number' => 10,
'order' => 'asc',
'orderby' => 'name',
'hide_empty' => true,
'parent' => 0,
);

if ( isset( $block->context['termQuery'] ) ) {
if ( ! empty( $block->context['termQuery']['taxonomy'] ) ) {
$taxonomy_param = $block->context['termQuery']['taxonomy'];
if ( is_taxonomy_viewable( $taxonomy_param ) ) {
$query['taxonomy'] = $taxonomy_param;
}
}
if ( isset( $block->context['termQuery']['exclude'] ) ) {
$excluded_term_ids = array_map( 'intval', $block->context['termQuery']['exclude'] );
$excluded_term_ids = array_filter( $excluded_term_ids );
$query['exclude'] = $excluded_term_ids;
}
if (
isset( $block->context['termQuery']['perPage'] )
&& is_numeric( $block->context['termQuery']['perPage'] )
) {
$per_page = absint( $block->context['termQuery']['perPage'] );
$offset = 0;

if (
isset( $block->context['termQuery']['offset'] )
&& is_numeric( $block->context['termQuery']['offset'] )
) {
$offset = absint( $block->context['termQuery']['offset'] );
}

$query['offset'] = ( $per_page * ( $page - 1 ) ) + $offset;
$query['number'] = $per_page;
}
if (
isset( $block->context['termQuery']['order'] )
&& in_array(
strtoupper( $block->context['termQuery']['order'] ),
array( 'ASC', 'DESC' ),
true
)
) {
$query['order'] = strtoupper( $block->context['termQuery']['order'] );
}
if ( isset( $block->context['termQuery']['orderby'] ) ) {
$query['orderby'] = $block->context['termQuery']['orderby'];
}
if (
isset( $block->context['termQuery']['hideEmpty'] )
&& ! $block->context['termQuery']['hideEmpty']
) {
$query['hide_empty'] = false;
}
if (
is_taxonomy_hierarchical( $query['taxonomy'] )
&& isset( $block->context['termQuery']['parent'] )
) {
$query['parent'] = $block->context['termQuery']['parent'] || 0;
}
}

/**
* Filters the arguments which will be passed to `WP_Term_Query` for the Terms Query Block.
*
* Anything to this filter should be compatible with the `WP_Term_Query` API to form
* the query context which will be passed down to the Terms Query Block's children.
* This can help, for example, to include additional settings or meta queries not
* directly supported by the core Terms Query Block, and extend its capabilities.
*
* Please note that this will only influence the query that will be rendered on the
* front-end. The editor preview is not affected by this filter. Also, worth noting
* that the editor preview uses the REST API, so, ideally, one should aim to provide
* attributes which are also compatible with the REST API, in order to be able to
* implement identical queries on both sides.
*
* @since 6.9.0
*
* @param array $query Array containing parameters for `WP_Term_Query` as parsed by the block context.
* @param WP_Block $block Block instance.
* @param int $page Current query's page.
*/
return apply_filters( 'terms_query_block_query_vars', $query, $block, $page );
}
}
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.9/block-bindings.php';
require __DIR__ . '/compat/wordpress-6.9/post-data-block-bindings.php';
require __DIR__ . '/compat/wordpress-6.9/term-data-block-bindings.php';
require __DIR__ . '/compat/wordpress-6.9/terms-query-block.php';
Copy link
Contributor

Choose a reason for hiding this comment

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

Furthermore, these two PHP files should have an additional check for the experimental settings:

if ( gutenberg_is_experiment_enabled( 'gutenberg-block-experiments' ) ) {
	require __DIR__ . '/experimental/term-data-block-bindings.php';
	require __DIR__ . '/experimental/terms-query-block.php';
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It also looks like these were improperly located within the WP_REST_Controller check if I'm reading the comment These files only need to be loaded if within a rest server instance correctly, so I've moved them both outside. @t-hamano Let me know if I've done that incorrectly.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you, all files and code seem to be placed correctly.

Copy link
Contributor Author

@cr0ybot cr0ybot Sep 27, 2025

Choose a reason for hiding this comment

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

I'm pretty sure that as a result, this PR could be marked as No Core Sync Required? There is already a core backport PR open for the original Terms Query block PR #70720 but since it's been marked as experimental I assume that PR will stay open until the experimental flag is removed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it's fine to prepare a core backport PR beforehand, but it should not be committed if the feature is still experimental. For now, this backport changelog file should also be deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@t-hamano Thanks, I've removed that too.

require __DIR__ . '/compat/wordpress-6.9/rest-api.php';
require __DIR__ . '/compat/wordpress-6.9/class-gutenberg-hierarchical-sort.php';

Expand Down
14 changes: 8 additions & 6 deletions packages/block-library/src/term-template/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
"ancestor": [ "core/terms-query" ],
"description": "Contains the block elements used to render a taxonomy term, like the name, description, and more.",
"textdomain": "default",
"usesContext": [ "termQuery", "termsToShow" ],
"attributes": {
"namespace": {
"type": "string"
}
},
"usesContext": [
"termId",
"termData",
"termQuery",
"postId",
"templateSlug",
"previewTaxonomy"
],
"supports": {
"reusable": false,
"html": false,
Expand Down
Loading
Loading