Best way to filter records by one of its tags?

I need to tag my blog post records with certain tags like automotive, aerospace, etc… So it is basically a field with an array of values. What would be the best way then to GraphQL these posts by one of its tags?

Thanks

Hello @primoz.rome

I would say the best way to go about it would be to use a “Single line string” field, with presentation set to “Radio Group” (in case of single tags) or “Select input” (in case of multiple tags)

The editors will then have a nice interface to select between tags, and you can filter them with the “anyIn” or with a regex in the CDA

1 Like

Thank @m.finamor.

or “Select input” (in case of multiple tags)

I guess, I need to install a Plugin to handle multiple selection in the Select input? Default Select UI only allows to select one value…

Sorry @primoz.rome !!! I mixed them up: both of those are for single tags, for multiple tags you can use this one: Tag editor - Plugins - DatoCMS

Yes, no problem. I already went this way, but I can not select the plugin you suggested on the Single line text field:

Installed plugin:

Field setup:

Am I missing something?

1 Like

@primoz.rome for strings you need to add a custom rule on the plugin page:

This makes i so every single line string field with the api key “tags” will have the plugin applied to it

1 Like

Ah okay, so I need to specify in the settings, where this will be applied? I though I can also apply it manually to a specific fields. No worries I will try this way…

@m.finamor How does anyIn filter work? I tried this but it doesn’t work.

query MyQuery {
  allBlogPosts(filter: {tags: {anyIn: "Monitoring"}}) {
    title
    tags
    slug
  }
}

Then I tried in filter which is available, but this does not find the tag:

query MyQuery {
  allBlogPosts(filter: {tags: {in: "Monitoring"}}) {
    title
    tags
    slug
  }
}

UPDATE 1: I don’t think anyIn filter will work on Single line fields…

UPDATE 2: This works, but it doesnt find this record "tags": "Monitoring, Data Recording, Vehicle Testing"

query MyQuery {
  allBlogPosts(filter: { tags: { in: ["Monitoring"] } }) {
    title
    tags
    slug
  }
}

Base on the documentation here would be best to create a new content type Tag and then use link fields to mark blog posts… Link fields can be filtered with anyIn filter.

UPDATE: I was wrong :man_facepalming:. anyIn filter on links fields searches only by itemId.

I am stuck with this :sweat_smile:

The best way to filter if you are using a string would be with a pattern.
The tags are gonna be separated by a comma and a space, so lets say you want to get all posts with the tag tagone you would run the following query:

Now if i want only the records that have both tagone and tagtwo in them i would do the following query:

Yes this works partly, but it is far from ideal… In your case it would never find this

query MyQuery {
  allBlogPosts(filter: { tags: { matches: { pattern: "tagone, tagthreee" }}}) {
    title
    tags
    slug
  }
}

And without that this kind of tag filtering is not useful at all…

In that case you would have to use a Regex to match that pattern:

However, as you said, if using regex generates too much unnecessary complexity for the queries, then you can create a new “tag” model, and link its “tag records” through a “Multiple links” field, and then filter them based on the “allIn” (or “anyIn”/“notIn”) filter and abandon the single line string approach alltogether