Terje Nygård
Terje Nygård

Reputation: 1257

Contentful JS API: Query for related articles with similar tags as the currently viewed article ? (article…tagList[])

Overview:

Someone views an article (contenttype : article). This article has a list of tags (contenttype : articletags).

Based on the linked tags in this article, I am trying to do a query for articles which has one or more of the same tags linked to it (call it Related Articles with same tags).

Here is the structure:

let tags = article.items[0].fields.tagsList.map(tag => {
   return tag.fields.navn
})

Which I need to query for articles like this:

contentful.getEntries({content_type: ‘article’}) ???

How can I query for the ids?

Upvotes: 0

Views: 1123

Answers (2)

reggi49
reggi49

Reputation: 560

You can use the [in] operator to query for entries with a given tag.

    contentful
      .getEntries({
        content_type: "article",
        'fields.tags':  "website" , //tag id = website
      })
      .then(({ items }) => {
        console.log(items);
      })
      .catch(console.error);

reference: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/inclusion

Upvotes: 1

stefan judis
stefan judis

Reputation: 3879

I think what you're looking for is the links_to query parameter.

With this one you can figure out which entries relate to another one.

client.getEntries({
  links_to_entry: '<entry_id>'
})

Upvotes: 1

Related Questions