Valores meta
Leia mais no guia Trabalhando com valores meta.
Estes são exemplos de queries para buscar metadados e filtrar resultados por meta.
Consultando meta
Buscar o valor meta simples _thumbnail_id de posts:
{
posts {
id
title
metaValue(key: "_thumbnail_id")
}
}Buscar o valor meta array upvotes de comentários:
{
comments {
id
content
upvotes: metaValues(key: "upvotes")
}
}Filtrando por meta
Filtrar posts onde a chave meta _thumbnail_id existe:
{
posts(filter: {
metaQuery: {
key: "_thumbnail_id",
compareBy:{
key: {
operator: EXISTS
}
}
}
}) {
id
title
metaValue(key: "_thumbnail_id")
}
}Filtrar usuários onde a entrada meta nickname tem um determinado valor:
{
users(filter: {
metaQuery: {
key: "nickname",
compareBy:{
stringValue: {
value: "leo"
operator: EQUALS
}
}
}
}) {
id
name
metaValue(key: "nickname")
}
}Filtrar comentários onde a entrada meta upvotes (que é um array de inteiros) tem os valores 4 ou 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")
}
}Adicionando meta
Você pode adicionar entradas meta a custom posts, tags, categorias, comentários e usuários.
Esta query adiciona uma entrada meta ao post com 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")
}
}
}Esta query adiciona a mesma chave meta com valores diferentes a posts diferentes, em lote:
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")
}
}
}Atualizando meta
Atualizar uma entrada meta de uma categoria:
mutation {
updateCategoryMeta(input: {
id: 20
key: "_source"
value: "Updated source value"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
category {
__typename
id
metaValue(key: "_source")
}
}
}Esta query usa mutations aninhadas para atualizar um valor meta em um 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")
}
}
}
}Excluindo meta
Excluir uma entrada meta de um post:
mutation {
deletePostMeta(input: {
id: 5
key: "some_key"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
metaValue(key: "some_key")
}
}
}Excluir a mesma entrada meta de múltiplos posts:
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")
}
}
}Definindo múltiplas entradas meta de uma vez
Você pode definir múltiplas entradas meta de uma vez passando um JSON para as diferentes mutations 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"])
}
}
}Definindo entradas meta ao criar/atualizar uma entidade
Você pode definir entradas meta diretamente ao criar ou atualizar um custom post, tag, categoria ou comentário, via parâmetro meta.
Esta query define os meta ao adicionar um comentário:
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"])
}
}
}Esta query injeta os meta na mutation aninhada 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")
}
}
}
}