Score:2

Drupal JSON:API - retrieving nodes tagged by multiple terms

cn flag

Let's say I have an articles content type, and a field_tags reference field (with multiple values referencing a "tag" vocabulary)

To retrieve articles referencing, say, "tag1" or "tag2", I do this:

jsonapi/node/article?
&filter[tags][condition][path]=field_tags.id
&filter[tags][condition][operator]=IN
&filter[tags][condition][value][]=id-of-tag1
&filter[tags][condition][value][]=id-of-tag2

That gives me all articles that have either tag1 or tag2.

However, How can I get all articles that have both tag1 and tag2?

Score:0
pw flag

I have tried as follow

/jsonapi/node/article?filter[field_tags.meta.drupal_internal__target_id]=5&filter[field_tags.meta.drupal_internal__target_id]=6

enter image description here

you can see in the screen shot only 1 node filtered which has both 5 & 6 term id.

Hubert avatar
cn flag
Thanks for the reply! When I try this it only returns nodes that have the last value in the filter. So, in your example I think this would only return nodes which have the term id of 6 . In your test, did you have nodes that had only the term id 6? I think they will be returned with that query.
khurrami avatar
pw flag
Yes you are right when i added node only with term id 6 that is also filtered.
Score:0
cn flag

You can use condition grouping and have like below :

?filter[tag-group][group][conjunction]=AND

&filter[tagA-filter][condition][path]=field.tags.id
&filter[tagA-filter][condition][value]=id-of-tag1
&filter[tagA-filter][condition][memberOf]=tag-group

&filter[tagB-filter][condition][path]=field.tags.id
&filter[tagB-filter][condition][value]=id-of-tag2
&filter[tagB-filter][condition][memberOf]=tag-group

I have not tried that but I think you can even use a code like this:

?filter[tagA-filter][condition][path]=field.tags.id
&filter[tagA-filter][condition][value]=id-of-tag1
&filter[tagB-filter][condition][path]=field.tags.id
&filter[tagB-filter][condition][value]=id-of-tag2

just notice that in this case two different key is used for conditions

Hubert avatar
cn flag
Unfortunately that doesn't seem to work. When I try it doesn't return any result.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.