Skip to content

DataViews: Query filters on taxonomy fields by the taxonomy - #86458

Merged
andrewserong merged 6 commits into
WordPress:trunkfrom
padams:add/taxonomy-filters-query
Nov 10, 2026
Merged

andrewserong merged 6 commits into
WordPress:trunkfrom
padams:add/taxonomy-filters-query

Conversation

@padams

@padams padams commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

What?

See #74865

Filters on taxonomy fields reach the query in the DataViews screens that list posts: the media modal and the post list route. A getTaxonomyQueryArgs() function maps them onto the taxonomy parameters the REST API already accepts.

Why?

Both screens turn a view's filters into REST parameters with filter.field === … branches for their own fields. A filter on any other field is in view.filters but never reaches the request. Taxonomies are the most common fields a plugin or site adds, and the posts controller already accepts any of, all of and none of for every show_in_rest taxonomy (prepare_tax_query()), so the query side only needs the mapping.

How?

Both screens are behind experiments, so each has its own copy of getTaxonomyQueryArgs( filters, taxonomies ), and @wordpress/views is unchanged until there's a home for the mapping, such as a public viewToQuery or field registration. It treats a field whose ID is a taxonomy's REST base as that taxonomy's field. The REST base is the property that holds an item's term IDs in its REST record, so the field reads its value with no getValue, and it's the query parameter that filters by them, so no new field property is needed.

Operator Query
is, isAny {rest_base}=[ids]
isAll {rest_base}[terms]=[ids]&{rest_base}[operator]=AND
isNot, isNone {rest_base}_exclude=[ids]

A filter with no terms leaves the query alone. A filter matches the chosen terms only, not their child terms, because the REST API's include_children defaults to false. Including children can't work with isAll: WP_Tax_Query would then require every child term too.

  • MediaUploadModal reads the attachment taxonomies with getTaxonomies( { type: 'attachment' } ), only when the attachment post type has any, and merges the arguments into its query.
  • viewToQuery() in routes/post-list takes the post type's taxonomies, from getTaxonomies() in the stage and resolveSelect() in the route loader.

Two operators have no REST equivalent and are left out: isNotAll, and an empty or "not set" filter, which DataViews doesn't have.

Testing Instructions

Unit tests: npm run test:unit packages/media-utils. The post list route has no unit tests set up, so its copy is covered by the media modal's tests.

In the media modal, with #86457 and its test plugin (the field ID genres is the taxonomy's rest_base):

  1. Open the media modal from an Image block and add a Genres filter.
  2. "is any of" one genre: only images with it show.
  3. "is all of" two genres: only images with both show.
  4. "is none of" one genre: images with it are left out.

Testing Instructions for Keyboard

No change to the interface.

Use of AI Tools

Written with Claude Code (Claude Opus 5.5) and reviewed by me.

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

👋 Welcome

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @padams! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

🎉 Props

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @peter@peteradams.org.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: peter@peteradams.org.

Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
Co-authored-by: padams <padams@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Nov 6, 2026
@padams
padams marked this pull request as ready for review November 6, 2026 19:57
@ramonjd ramonjd added the [Type] Enhancement A suggestion for improvement. label Nov 9, 2026

@andrewserong andrewserong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a great idea, thanks for digging in @padams!

In terms of adding this in to the media upload modal and the routes/post-list I think we should do this.

My one hesitation is about placing the utility function in views and exposing it publicly at this stage. Is a JS utility the function the way we want consumers to handle building the query args? We don't yet have a public viewToQuery function so it feels slightly uneven to have this exposed. Another question is whether the mapping to query args should be part of the process of registering fields 🤔

If we want to get this landed sooner rather than later, one option might be to duplicate this logic in both the media upload modal and the post-list route as they're both UIs that are hidden behind experiments, then we wouldn't need to change the views package?

For where we might place this logic in a reusable way, I might just ping @ntsekouras and @oandregal here in case they have any feedback as I know they've been actively working on DataViews and registration, and they might have thoughts about where the mapping should occur.


Other than that, one minor question: I think registering a hierarchical taxonomy defaults include_children to false and we're not adding any special handling here for if include_children is set to true. Is that intentional? (For the purposes of media folders I assume we don't want to include children in the filtering, but just thought I'd check)

const taxonomies = useSelect( ( select ) => {
const { getPostType, getTaxonomies } = select( coreStore );
return getPostType( 'attachment' )?.taxonomies?.length
? getTaxonomies( { type: 'attachment', per_page: -1 } )

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does fetching taxonomies support pagination? I don't think so, so per_page might be redundant here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right, it isn't paginated: /wp/v2/taxonomies takes only context and type, and core-data has supportsPagination: false for it. I've removed per_page here and in the post-list route.

@github-actions github-actions Bot removed the [Package] Views /packages/views label Nov 9, 2026
@padams

padams commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @andrewserong! Duplicating it makes sense. I've moved the mapping into the media upload modal and the post-list route, and @wordpress/views is unchanged until there's a home for it, whether that's a public viewToQuery or part of field registration. The two copies say the other exists, and the tests are next to the modal's copy, since the post-list route has no unit test setup.

On include_children: it follows the REST API default (false), so a filter matches the chosen terms only. That suits folders, and each child term is still its own option in the filter. Including children can't work with "is all of", because WP_Tax_Query then requires a post to have every child term as well. I've added a test for this and a line in the description.

If the mapping does end up in field registration, include_children is a case for it: a hierarchical keyword taxonomy would want a filter to include child terms, so that "California" matches "San Jose", while folders wouldn't. I'd be glad to help with that.

Leave @wordpress/views unchanged until there is a home for the mapping,
such as a public viewToQuery or field registration. Note that a filter
matches the chosen terms only, as include_children defaults to false.
The taxonomies endpoint isn't paginated, so every taxonomy comes back
without it.
@padams
padams force-pushed the add/taxonomy-filters-query branch from 9f45ff1 to bd46cf6 Compare November 9, 2026 16:33

@andrewserong andrewserong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the updates! This is looking good to me now, and I like that it's duplicated in the same kind of of file, too (taxonomy-query.ts) as it'll make it a clean process for refactoring if/when we decide to consolidate further down the track. Testing well for me, with the queries filtering results as expected:

Image

I reckon this is good to land, and we can continue iterating in follow-ups, too.

LGTM 🚢

@andrewserong
andrewserong merged commit 29bc9ed into WordPress:trunk Nov 10, 2026
76 checks passed
@github-actions github-actions Bot added this to the Gutenberg 24.5 milestone Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Media Utils /packages/media-utils [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants