Skip to content

Query Media Items

For components like the MediaGallerySection or the MediaList you need a selection of media items. There are three different ways how to create a selection.

  • getEntry: Astro function to load a single item of an Astro content collection.
  • getCollection: Astro function to filter items of an Astro content collection.
  • getMediaItems: LightNet function to simplify filtering media items.

Here are examples for creating a selection of media items.

---
import { getEntry } from "astro:content"
const items = [
await getEntry("media", "faithful-freestyle--en"),
await getEntry("media", "ollie-with-integrity--en"),
]
---

This example creates a selection of two media items by their identifiers using Astro’s getEntry. Find more information on getEntry and getCollection on the Astro docs on content collections.

---
import { getMediaItems } from "lightnet/content"
const items = await getMediaItems({
where: { type: "book", language: "en" },
orderBy: "dateCreated",
limit: 10
})
---

The getMediaItems function helps to query media items. It takes a sql-like query object as an argument. The example filters for all media items that have the media-type book and the language en. It orders them by the date they were created (newest first) and limits the result to 10 items.

The getMediaItems function accepts this parameters:

type: Object
example: { type: "book" }
required: false

Defines the attributes of the media items to be returned. Only media items with all matching attributes are returned.

type: string
example: "book"
required: false

Sets the media item type to be returned.

type: string
example: "en"
required: false

Sets the media item content language (as BCP-47 code) to be returned.

type: string
example: "family"
required: false

Sets the media item category to be returned.

type: string
example: "my-collection"
required: false

Sets the media item collection to be returned.

type: "dateCreated" | "title"
example: "dateCreated"
required: false

Sets the sort order of the returned media items:

  • dateCreated - sorts based on the dateCreated attribute from newest to oldest.
  • title - sorts based on the media item title
  • none - sorts based on the media item identifier (as defined by the content JSON file name), the order of collection items is taking precedence.

type: number
example: 42
required: false

Sets the maximum amount of media items to be returned.