cms
cms copied to clipboard
rest api filtering not working with taxonomy conditions
Bug description
I've tried to use the rest api in combination with taxonomy conditions, in order to get a list of entries from within a collection with just a specific taxonomy. while simple conditions like f.ex. title fields are working, the more complex taxonomy conditions are always returning all entries (also including entries with different taxonomies, not the one filtered by).
How to reproduce
-
create a new collection for entries (in my case a collection called "works")
-
create a taxonomy and entries with the new taxonomy selected (in my example the taxonomy is called "categories" and the term name "events"
-
try to call the api-url to get the filtered entries, in my case:
/api/collections/works/entries?filter[taxonomy:categories]=events
...the above api call always seems to be giving back all entries, also including entries without the 'events' category and/or including other categories. I've also tried with different namings (taxonomies, category, switching singulars to plurals, etc).
Logs
No response
Versions
Statamic 3.3.12 Pro Laravel 8.83.13 PHP 8.0.19 No addons installed
Installation
Fresh statamic/statamic site via CLI
Antlers Parser
No response
Additional details
No response
If I remember correctly what you're trying to do works only if the categories
field of the Works
collection allows only single selection. It won't work for multiple selection taxonomy fields.
You can try filter[categories:contains]=events
but that will also bring items with tags like summer-events
so take care.
Personally I just pulled all the entries and did my filtering on Vue.js. I still think this is a valid issue though.
You can try
filter[categories:contains]=events
but that will also bring items with tags likesummer-events
so take care.
@afonic thank you!! the :contains workaround is actually perfect for my use case (no tags or other taxonomies with similar namings used) – and same, would be using the other workaround for other projects, although it would be nice to be able to use the API (also in combination with pagination) for that and not let the frontend carry the heavy weight depending on the number of entries – will leave this issue open for that for now.
Please add support to filter by taxonomies via Content-API. Using taxonomies to filter content is an important concept in Statamic, so it should work in the API as expected. Thanks!
If I remember correctly what you're trying to do works only if the
categories
field of theWorks
collection allows only single selection. It won't work for multiple selection taxonomy fields.You can try
filter[categories:contains]=events
but that will also bring items with tags likesummer-events
so take care.Personally I just pulled all the entries and did my filtering on Vue.js. I still think this is a valid issue though.
You can prevent unwanted results (e.g. when searching for 'some' also returning 'something') by wrapping your value in double quotes. When calling ?filter[random-tax:contains]="some"
it will not return values containing "something". This is because the TestLike encodes the array to json, which wraps strings in double quotes: https://github.com/statamic/cms/blob/d9de08d08623dff928d7cd2b5d7fada4ba485924/src/Query/Builder.php#L628
Is there any update without workaround?