Fetching multi-model collections

Fetching multiple model collections

When you want to retrieve content items from multiple models, you can use the ContentItems query.

Include the __typename argument to see which model the returned content items belong to. You can make your query more specific by including the necessary arguments for each model. Please see the example below.

query {
    ContentItems( limit : 30 ) {
        items {
            __typename
            ... on Post {
                _id,
                _slug
            }
        }
    }
}

Query only content items from specific models

To narrow query results to specific models, you can include a filter on its name by using the _typename_any argument.

The following query retrieves all content items from the Post and Page models:

query {
  ContentItems( where : { _typename_any : [ "Post", "Page" ] } ) {
    items {
        ... on Post {
            _id,
            _slug
        }
        ... on Page {
            _id,
            _slug
        }
    }
  }
}

Additionally, you can also use multiple filters to retrieve only specific content items from a model. Read more in Filtering collections.

Arguments

The following arguments are available or required when querying a collection:

argumenttyperequireddescription
localeStringfalseLocale of a content item. If no locale is set, the default locale will be used.
locales[String!]falseOptional fallback locales.
sortSortInputTypefalseRead more in Sorting collections.
whereWhereInputTypefalseRead more in Filtering collections.
_typename_any[String]falseAllows filtering content items based on the given types.
people_also_viewed_idStringfalseAllows recommending multi-type content based on the given IDs. Read more in Fetching People Also Viewed content.

Advanced multi-type filtering

For our enterprise customers we support advanced filters on the multi-type query, please contact us at prepr.io/support (opens in a new tab) for more information.

Filtering multi-type collections

To filter multi-types collections, include the where argument in your query, followed by the relevant filter types for the fields on your model. Check out the full list of the available filters below.

filterdocumentation
IDRead more
SearchRead more
SlugRead more
TagsRead more
Content TypeRead more

Query by Customer Relation

The engagement your customers have with your content items can be tracked using the Prepr Tracking Code. If customers are allowed to bookmark, subscribe or like content items, this filter allows you to make a collection of the content items the customer has for example liked.

Read more about Customer Relation filtering.

Sorting multi-type collections

When fetching multiple content items at once, you can define the order of returned records by using the sort argument. This can be particularly useful when working with large datasets or complex queries. It helps to quickly locate specific records in your project.

You can order results in ascending or descending order by specific system fields. More information can be found below.

When the sort attribute is a field with the same value in multiple content items, the query will return the results in a random order when filters are included in the query.

Sorting by common attributes

It's possible to sort by the common attributes created, changed and published dates in ascending or descending order. The current values for sorting those fields are created_on_ASC, created_on_DESC, changed_on_ASC, changed_on_DESC, publish_on_ASC, publish_on_DESC.

type Query {
  ContentItems( sort : publish_on_DESC ) {
    items {
        _id,
        title
    }
  }
}

Sorting by popularity

This option will sort the content items on descending order by the number of views.

type Query {
  ContentItems( sort : popular ) {
    items {
        _id,
        title
    }
  }
}

Default ordering

If you don't pass an explicit order value the returned content items are ordered by publish_on_DESC. This means that the most recently published content item appear at the top of the list.

Was this article helpful?

We’d love to learn from your feedback