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
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
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?
@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
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 . anyIn
filter on links fields searches only by itemId
.
I am stuck with this
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