Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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: 0 additions & 3 deletions backport-changelog/6.9/9356.md

This file was deleted.

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
109 changes: 109 additions & 0 deletions lib/experimental/terms-query-block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?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,
'hierarchical' => false,
);

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 );
}
}
7 changes: 6 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.9/template-activate.php';
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/rest-api.php';
require __DIR__ . '/compat/wordpress-6.9/class-gutenberg-hierarchical-sort.php';

Expand Down Expand Up @@ -173,3 +172,9 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/experimental/interactivity-api/class-gutenberg-interactivity-api-full-page-navigation.php';
Gutenberg_Interactivity_API_Full_Page_Navigation::instance();
}

// Block Experiments.
if ( gutenberg_is_experiment_enabled( 'gutenberg-block-experiments' ) ) {
require __DIR__ . '/experimental/term-data-block-bindings.php';
require __DIR__ . '/experimental/terms-query-block.php';
}
2 changes: 1 addition & 1 deletion packages/block-library/src/query/edit/query-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { store as coreStore } from '@wordpress/core-data';
*/
import EnhancedPaginationControl from './inspector-controls/enhanced-pagination-control';
import { unlock } from '../../lock-unlock';
import { getQueryContextFromTemplate } from '../../utils/get-query-context-from-template';
import QueryInspectorControls from './inspector-controls';
import EnhancedPaginationModal from './enhanced-pagination-modal';
import { getQueryContextFromTemplate } from '../utils';
import QueryToolbar from './query-toolbar';

const { HTMLElementControl } = unlock( blockEditorPrivateApis );
Expand Down
60 changes: 1 addition & 59 deletions packages/block-library/src/query/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
* Internal dependencies
*/
import { terms } from './fixtures';
import {
getEntitiesInfo,
getValueFromObjectPath,
getQueryContextFromTemplate,
} from '../utils';
import { getEntitiesInfo, getValueFromObjectPath } from '../utils';

describe( 'Query block utils', () => {
describe( 'getEntitiesInfo', () => {
Expand Down Expand Up @@ -65,58 +61,4 @@ describe( 'Query block utils', () => {
expect( result ).toBe( 'test' );
} );
} );

describe( 'getQueryContextFromTemplate', () => {
it( 'should return the correct query context based on template slug', () => {
expect( getQueryContextFromTemplate() ).toStrictEqual( {
isSingular: true,
} );
expect( getQueryContextFromTemplate( '404' ) ).toStrictEqual( {
isSingular: true,
templateType: '404',
} );
expect( getQueryContextFromTemplate( 'blank' ) ).toStrictEqual( {
isSingular: true,
templateType: 'blank',
} );
expect( getQueryContextFromTemplate( 'single' ) ).toStrictEqual( {
isSingular: true,
templateType: 'single',
} );
expect(
getQueryContextFromTemplate( 'single-film' )
).toStrictEqual( {
isSingular: true,
templateType: 'single',
} );
expect( getQueryContextFromTemplate( 'page' ) ).toStrictEqual( {
isSingular: true,
templateType: 'page',
} );
expect( getQueryContextFromTemplate( 'wp' ) ).toStrictEqual( {
isSingular: true,
templateType: 'custom',
} );
expect( getQueryContextFromTemplate( 'category' ) ).toStrictEqual( {
isSingular: false,
templateType: 'category',
} );
expect(
getQueryContextFromTemplate( 'category-dog' )
).toStrictEqual( {
isSingular: false,
templateType: 'category',
} );
expect( getQueryContextFromTemplate( 'archive' ) ).toStrictEqual( {
isSingular: false,
templateType: 'archive',
} );
expect(
getQueryContextFromTemplate( 'archive-film' )
).toStrictEqual( {
isSingular: false,
templateType: 'archive',
} );
} );
} );
} );
29 changes: 0 additions & 29 deletions packages/block-library/src/query/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,32 +492,3 @@ export const useUnsupportedBlocks = ( clientId ) => {
[ clientId ]
);
};

/**
* Helper function that returns the query context from the editor based on the
* available template slug.
*
* @param {string} templateSlug Current template slug based on context.
* @return {Object} An object with isSingular and templateType properties.
*/
export function getQueryContextFromTemplate( templateSlug ) {
// In the Post Editor, the template slug is not available.
if ( ! templateSlug ) {
return { isSingular: true };
}
let isSingular = false;
let templateType = templateSlug === 'wp' ? 'custom' : templateSlug;
const singularTemplates = [ '404', 'blank', 'single', 'page', 'custom' ];
const templateTypeFromSlug = templateSlug.includes( '-' )
? templateSlug.split( '-', 1 )[ 0 ]
: templateSlug;
const queryFromTemplateSlug = templateSlug.includes( '-' )
? templateSlug.split( '-' ).slice( 1 ).join( '-' )
: '';
if ( queryFromTemplateSlug ) {
templateType = templateTypeFromSlug;
}
isSingular = singularTemplates.includes( templateType );

return { isSingular, templateType };
}
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