Repository navigation
DataViews: Query filters on taxonomy fields by the taxonomy - #86458
Conversation
🤖 PR meta 🤖👋 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. 🎉 PropsUnlinked AccountsThe 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. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the |
andrewserong
left a comment
There was a problem hiding this comment.
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 } ) |
There was a problem hiding this comment.
Does fetching taxonomies support pagination? I don't think so, so per_page might be redundant here.
There was a problem hiding this comment.
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.
|
Thanks @andrewserong! Duplicating it makes sense. I've moved the mapping into the media upload modal and the post-list route, and On If the mapping does end up in field registration, |
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.
9f45ff1 to
bd46cf6
Compare
andrewserong
left a comment
There was a problem hiding this comment.
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:
I reckon this is good to land, and we can continue iterating in follow-ups, too.
LGTM 🚢
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 inview.filtersbut 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 everyshow_in_resttaxonomy (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/viewsis unchanged until there's a home for the mapping, such as a publicviewToQueryor 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 nogetValue, and it's the query parameter that filters by them, so no new field property is needed.is,isAny{rest_base}=[ids]isAll{rest_base}[terms]=[ids]&{rest_base}[operator]=ANDisNot,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_childrendefaults to false. Including children can't work withisAll:WP_Tax_Querywould then require every child term too.MediaUploadModalreads the attachment taxonomies withgetTaxonomies( { type: 'attachment' } ), only when the attachment post type has any, and merges the arguments into its query.viewToQuery()inroutes/post-listtakes the post type's taxonomies, fromgetTaxonomies()in the stage andresolveSelect()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
genresis the taxonomy'srest_base):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.