Media

These are examples of queries to fetch and mutate media data.

Fetching media permalink

A media item, with the image source in different sizes:

query {
mediaItem(by: { id: 1647 }) {
id
srcSet
src
thumbSizeSrc: src(size: "thumbnail")
largeSizeSrc: src(size: "large")
}
}

All media items (with mime type "image" by default), and all the image sizes:

query {
imageSizeNames
mediaItems {
id
srcSet
src(size: "medium")
sizes(size: "thumbnail")
width
height
slug
url
urlAbsolutePath
title
caption
altText
description
date
mimeType
}
}

The posts' featured image:

query {
posts {
id
hasFeaturedImage
featuredImage {
id
src
width
height
}
}
}

Filtering media items:

{
mediaItems(
pagination: { limit: 3 },
sort: { by: TITLE },
filter: { dateQuery: { after: "2012-01-02" } }
) {
id
src
height
width
}
}

Fetching video and audio items:

{
mediaItems(
filter: { mimeTypes: ["video", "audio"] }
) {
id
src
}
}

We can reference any image existing in the Media library.

mutation {
setFeaturedImageOnCustomPost(
input: {
customPostID: 1499,
mediaItemID: 1505
}
) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
customPostID
customPost {
...on WithFeaturedImage {
featuredImage {
id
src
}
}
}
}
}

With nested mutations:

mutation {
post(by: { id: 1499 }) {
setFeaturedImage(input: { mediaItemID: 1647 }) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
customPostID
customPost {
...on WithFeaturedImage {
featuredImage {
id
src
}
}
}
}
}
}