I'm trying to do something through the Contentful API (npm module of the content delivery) and I'm not sure if it's possible.
I have a content type tag
in Contentful which has 1 field, also called tag
. I then have another content type called article
which has a multiple reference field called tags
. This field allows an editor to link multiple tag entries to the article.
Now when the user loads the article, I want to display similar reading by using the tags to find other articles with the same tags.
So the relevant JSON structure of an article looks something like this:
{
sys: {...},
fields: {
tags: [
{
sys: {...},
fields: { tag: "Football" }
},
{
sys: {...},
fields: { tag: "Leeds United" }
},
{
sys: {...},
fields: { tag: "Elland Road" }
}
]
}
}
So say the article the user is reading has the tags football
, Leeds United
and Elland Road
. How would I use the API to return other articles with those 3 tags, or some of those tags. For example and article with all 3 tags would be a strong match, but an article with just 1 matching tag would be weak.
I tried using the inclusion rule like so:
contentClient.entries({
"content_type": "xxxxxxxx",
"fields.tags[in]": ["football", "Leeds United"],
}, function(err, entries){
if (err) throw err;
console.log(entries);
});
This of course doesn't work because the field with the value in is not fields.tags[in]
it's fields.tags.fields.tag
.