Skip to content

Query media items

For components like MediaGallerySection or MediaList, you need a selection of media items. There are three different ways to create that 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 of how to create 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 about getEntry and getCollection in 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 makes it easier to query media items. It takes an 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 these 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 takes precedence.

type: number
example: 42
required: false

Sets the maximum number of media items to return.