Search code examples
strapi

Strapi query for relation with IN filter and AND clause


I have a collection blogs that has&belongs to many categories which have a title field.

I want to query blogs for results that have at least two specific category titles: foo and bar.

my blogs has the following entries:

  • 2 entry with 0 categories
  • 1 entry with 1 category: foo
  • 1 entry with 2 categories: foo, bar

I've tried:

GET /blogs?_where[0][categories.title_in]=foo&_where[1][categories.title_in]=bar

--> 0 results

GET /blogs?[categories.title_in]=foo&[categories.title_in]=bar

--> 2 results

GET /blogs?[categories.title_in][0]=foo&[categories.title_in][1]=bar

--> 2 results

Basically I cannot get the AND condition to take. Filtering for both having foo AND bar categories should return only 1 result (the last one in the example)

How do I do that?


Solution

  • This seem to work for me in Strapi v4

    multiple AND Filter

    localhost:1337/api/artworks?publicationState=live&sort[0]=name&fields[0]=name&fields[1]=slug&fields[2]=price&fields[3]=width&fields[4]=height&populate[0]=image&populate[1]=artist&populate[2]=artwork_art_type&populate[3]=artwork_format&populate[4]=artwork_subject&populate[5]=artwork_technique&pagination[page]=&pagination[pageSize]=40&filters[$and][0][artwork_art_type][name][$eq]=${artworkArtTypeValue}&filters[$and][0][artwork_format][name][$eq]=${artworkFormatValue}
    

    IN filter display all except with current ID

    /artworks?publicationState=live&populate[0]=image&filters[artist][id][$eq]=${artworkArtistId}&filters[id][$ne]=${artworkId}&pagination[pageSize]=100