Contentful logo

Contentful Community

How to query on multiple content types

I have various Content Types that represent article pages on a site. For the sake of example, I’ll say they’re as follows:

StoryArticle
  slug
SlideShowArticle
  slug
NewsArticle
  slug

All of these types have a slug field. So when a user loads a page of the site, we need to take the slug and find the corresponding Content.

So for a page of http://www.example.com/articles/latest-news-article

I’d query the API with the following parameters:

fields.slug=latest-news-article

However, doing so requires, per the API docs, that a “content_type” be specified, and I can only provide a single content type.

When querying entries and using search filters based on fields or ordering by fields you must specify this parameter. You can only specify one value for content_type at a time.

Why can I only specify one value for content_type? I know this page could be one of 3 content types, and I’d like to search across all 3, not just one.

How do I work around this?

The way to solve this is with a common content type, maybe called Article, which has the fields common to all 3 of your article content types including the slug, and a single reference to a StoryArticle, SlideShowArticle, or NewsArticle (which should only have the specific content relating to a story, slideshow, or news piece).

That way you can have a single content type, Article, to search for the slug, but still get all the information specific to the particular article type.

Hey @jason1,

In general, it is possible to specify entries of multiple content types by specifying a search parameter on the lines of the following:

sys.contentType.sys.id[in]=[content_type1],[content_type2],...

As shown in the following example:

https://cdn.contentful.com/spaces/cfexampleapi/entries?access_token=b4c0n73n7fu1&sys.contentType.sys.id[in]=cat,dog

Reference link

However, the issue here is that each field is defined on the basis of a single content type.

That being the case, although your fields may have the same name across multiple content types, they will effectively be different entities, so it’s unfortunately not possible to search through the fields of different content types that you may have specified in your query.

So, as Charlie greatly explained, you may want to create a common content type.

Hope this helps :grinning:

@charlie That’s a solve, but that means I’ll have to update my data model (in a way that’s really going to confuse things) AND it will affect the content editing experience just to work out an API limitation. That’s definitely not ideal.

1 Like

Hi there, one year later, is this still the case? I am using the .NET core api, and I’m not able to get that sys.contentType.sys.id query to work, it throws before making the query with the error that I need to set A content type id.

We are in the beginning of our project, and don’t have much content yet so I guess I could make a common entity and then link the “sub-entity” in via reference. Hows the performance doing that way?

Hi @ullmark,
Could you tell us how exactly you’re trying to structure your query?

@gabriel I actually got some additional requirements and ended up structuring it like @charlie suggested. Worked out nicely.

Glad to hear that. Let me know if you have any other questions :slight_smile: