Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
guard against redeclaration
  • Loading branch information
cr0ybot committed Sep 26, 2025
commit 344e8668a2bcb784cf9a761ac7aad96b32d52fbc
178 changes: 90 additions & 88 deletions lib/compat/wordpress-6.9/terms-query-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,100 +7,102 @@
* @subpackage Blocks
*/

/**
* 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 ( ! 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'] ) ) {
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']['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']['offset'] )
&& is_numeric( $block->context['termQuery']['offset'] )
isset( $block->context['termQuery']['order'] )
&& in_array(
strtoupper( $block->context['termQuery']['order'] ),
array( 'ASC', 'DESC' ),
true
)
) {
$offset = absint( $block->context['termQuery']['offset'] );
$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;
}

$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 );
/**
* 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 );
}
}
Loading