Repository navigation
[6.x] Don't filter Terms fieldtype values by collection usage - #15870
Merged
jasonvarga merged 1 commit intoNov 12, 2026
Merged
Conversation
The Terms fieldtype adds where('collection', ...) so terms get collection
scoped URLs, but that also limits results to terms used in the collection.
The ids come from the parent entry, so that filter can't change the result
for saved entries, and it's expensive for database driven term repositories.
Add withCollectionContext() to the term query builder, which supplies the
collection to the terms without filtering, and use it from the fieldtype when
the query builder supports it. Other builders keep the previous behavior.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related: statamic/eloquent-driver#561. Paired driver PR: statamic/eloquent-driver#649, which adds
withCollectionContext()to the Eloquent term query builder, along with other speedups that work on any cms version.The problem
When a Terms field is augmented on an entry,
Terms::queryBuilder()addswhere('collection', $parent->collectionHandle()). The reason is so the terms get collection-scoped URLs (/blog/tags/foo). But on the term query builder,where('collection')does two things:The second part can't change the result here: the ids come from the parent entry, which is in that collection. The only difference shows up in Live Preview: a term that no saved entry uses yet is hidden until you save. That's arguably a bug in itself.
For the Stache, the usage filter reads an index, so it's cheap. For database-backed term repositories it means scanning the entries table. In the eloquent driver that's a JSON
EXISTSper term in the taxonomy, and it runs for every Terms column on every row of the CP entries listing, and on every front-end augmentation. People with large sites report CP listings timing out after 60 s.The change
withCollectionContext($collection)to the StacheTermQueryBuilder. It attaches the collection to the resulting terms without filtering by usage.method_exists), and otherwise falls back towhere('collection')as before.Why this isn't breaking
where('collection')keeps its meaning for everyone else.first()(used formax_items: 1) goes throughget(), so the collection is attached there too.method_existsis false, so it falls back towhere('collection')Benchmarks
These use the real Terms fieldtype through the eloquent driver: 100 rows of the CP entries listing, and the Terms fields on an entry page. Each figure is the median of 3 fresh requests. The full matrix, the methodology and the benchmark script are in statamic/eloquent-driver#649.
🤖 Generated with Claude Code