Valori meta
Per saperne di più, consulta la guida Lavorare con i valori meta.
Questi sono esempi di queries per recuperare metadati e filtrare i risultati per meta.
Interrogare i meta
Recuperare il valore meta singolo _thumbnail_id dai post:
{
posts {
id
title
metaValue(key: "_thumbnail_id")
}
}Recuperare il valore meta array upvotes dai commenti:
{
comments {
id
content
upvotes: metaValues(key: "upvotes")
}
}Filtrare per meta
Filtrare i post in cui esiste la chiave meta _thumbnail_id:
{
posts(filter: {
metaQuery: {
key: "_thumbnail_id",
compareBy:{
key: {
operator: EXISTS
}
}
}
}) {
id
title
metaValue(key: "_thumbnail_id")
}
}Filtrare gli utenti in cui la voce meta nickname ha un determinato valore:
{
users(filter: {
metaQuery: {
key: "nickname",
compareBy:{
stringValue: {
value: "leo"
operator: EQUALS
}
}
}
}) {
id
name
metaValue(key: "nickname")
}
}Filtrare i commenti in cui la voce meta upvotes (che è un array di interi) ha i valori 4 o 5:
{
comments(filter: {
metaQuery: [
{
relation: OR
key: "upvotes",
compareBy: {
arrayValue: {
value: 4
operator: IN
}
}
},
{
key: "upvotes",
compareBy: {
arrayValue: {
value: 5
operator: IN
}
}
}
]}) {
id
content
upvotes: metaValues(key: "upvotes")
}
}Aggiungere meta
È possibile aggiungere voci meta a custom post, tag, categorie, commenti e utenti.
Questa query aggiunge una voce meta al post con ID 4:
mutation {
addCustomPostMeta(input: {
id: 4
key: "some_key"
value: "Some value"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
customPost {
id
metaValue(key: "some_key")
}
}
}Questa query aggiunge la stessa chiave meta con valori diversi a post diversi, in blocco:
mutation {
addCustomPostMetas(inputs: [
{
id: 4
key: "some_key"
value: "Some value"
},
{
id: 5
key: "some_key"
value: "Some other value"
},
{
id: 6
key: "some_key"
value: "Yet another value"
}
]) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
customPost {
id
metaValue(key: "some_key")
}
}
}Aggiornare i meta
Aggiornare una voce meta di una categoria:
mutation {
updateCategoryMeta(input: {
id: 20
key: "_source"
value: "Updated source value"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
category {
__typename
id
metaValue(key: "_source")
}
}
}Questa query utilizza mutazioni annidate per aggiornare un valore meta in un post:
mutation {
post(by: {id: 1}) {
updateMeta(input: {
key: "some_key"
value: "Updated description"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
metaValue(key: "single_meta_key")
}
}
}
}Eliminare i meta
Eliminare una voce meta da un post:
mutation {
deletePostMeta(input: {
id: 5
key: "some_key"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
metaValue(key: "some_key")
}
}
}Eliminare la stessa voce meta da più post:
mutation {
deletePostMetas(inputs: [
{
id: 5
key: "some_key"
},
{
id: 6
key: "some_key"
}
]) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
metaValue(key: "some_key")
}
}
}Impostare più voci meta contemporaneamente
È possibile impostare più voci meta contemporaneamente passando un JSON alle diverse mutazioni set{Entity}Meta:
mutation {
setCustomPostMeta(input: {
id: 4
entries: {
single_meta_key: [
"This is a single entry",
],
object_meta_key: [
{
key: "This is a key",
value: "This is a value",
},
],
array_meta_key: [
"This is a string",
"This is another string",
],
object_array_meta_key: [
[
{
key: "This is a key 1",
value: "This is a value 1",
},
{
key: "This is a key 2",
value: "This is a value 2",
},
]
],
}
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
customPost {
id
meta(keys: ["single_meta_key", "object_meta_key", "array_meta_key", "object_array_meta_key"])
}
}
}Impostare voci meta durante la creazione/aggiornamento di un'entitÃ
È possibile definire voci meta direttamente durante la creazione o l'aggiornamento di un custom post, tag, categoria o commento, tramite il parametro meta.
Questa query imposta i meta durante l'aggiunta di un commento:
mutation {
addCommentToCustomPost(input: {
customPostID: 1130
commentAs: { html: "New comment" }
meta: {
some_meta_key: [
"This is a single entry",
],
another_meta_key: [
"This is an array entry 1",
"This is an array entry 2",
],
}
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
comment {
id
meta(keys: ["some_meta_key", "another_meta_key"])
}
}
}Questa query inietta i meta nella mutazione annidata Post.update:
mutation {
post(by: {id: 1}) {
update(input: {
meta: {
single_meta_key: [
"This is an updated value",
]
}
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
metaValue(key: "single_meta_key")
}
}
}
}