Contentful logo

Contentful Community

Query not working searching entries with related document having tags xyz

A client of mine has a use case/project that uses 2 content types.
Let’s name them “Reports” and “Companies”.

A report has a company fields which links/relates to content of a single company.
I’m trying to fetch reports, where company has “tag1” or “tag2” tags.
I’m using the following syntax in JavaScript:

const result = await client
  .getEntries({
    content_type: process.env.CTF_REPORT_TYPE_ID,
    order: '-sys.createdAt',
    'fields.company.metadata.tags.sys.id[in]': 'tag1,tag2',
    limit: 4,
  })
  .catch(console.error)

Which will error out with following error:

Documentation:
If this isn’t the right way to look for tags in nested content types, could someone explain how to do this. I tried finding this use case in the documentation on how to solve this but without any luck.

Tries:
I don’t remember where exactly but I’ve read that I might need to provide the content_type for the company field, which I tried by adding:

'fields.company.content_type.sys.id': process.env.CTF_COMPANY_TYPE_ID,

Or also tried

'fields.company.content_type': process.env.CTF_COMPANY_TYPE_ID,

Which did not solve the issue at hand.

Help:
Any help would be greatly appreciated.
Thank you!

1 Like

Hi @Mathieu ,

I believe what you are trying to achieve is not possible with the current API structure.
It is possible to search on reference from reports to company and it is also possible to search on tags of an entry, but I don’t think it’s possible to do that combined. A potential solution could be to query all the Companies filtering by the tags and then do a second query for reports and check which companies are linked to them. It’s just an extra API call and it should do what you need.