Valores meta
Lee más en la guía Trabajando con valores meta.
Estos son ejemplos de consultas para obtener metadatos y filtrar resultados por meta.
Consultando meta
Obtener el único valor meta _thumbnail_id de los posts:
{
posts {
id
title
metaValue(key: "_thumbnail_id")
}
}Obtener el array de valores meta upvotes de los comentarios:
{
comments {
id
content
upvotes: metaValues(key: "upvotes")
}
}Filtrar por meta
Filtrar posts en los que existe la clave meta _thumbnail_id:
{
posts(filter: {
metaQuery: {
key: "_thumbnail_id",
compareBy:{
key: {
operator: EXISTS
}
}
}
}) {
id
title
metaValue(key: "_thumbnail_id")
}
}Filtrar usuarios donde la entrada meta nickname tiene un determinado valor:
{
users(filter: {
metaQuery: {
key: "nickname",
compareBy:{
stringValue: {
value: "leo"
operator: EQUALS
}
}
}
}) {
id
name
metaValue(key: "nickname")
}
}Filtrar comentarios donde la entrada meta upvotes (que es un array de enteros) tiene los valores 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")
}
}Añadir meta
Puedes añadir entradas meta a custom posts, etiquetas, categorías, comentarios y usuarios.
Esta consulta añade una entrada 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")
}
}
}Esta consulta añade la misma clave meta con valores distintos a distintos posts, en bloque:
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")
}
}
}Actualizar meta
Actualizar una entrada meta de categoría:
mutation {
updateCategoryMeta(input: {
id: 20
key: "_source"
value: "Updated source value"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
category {
__typename
id
metaValue(key: "_source")
}
}
}Esta consulta usa mutaciones anidadas para actualizar un valor meta en 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")
}
}
}
}Borrar meta
Borrar una entrada meta de un post:
mutation {
deletePostMeta(input: {
id: 5
key: "some_key"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
metaValue(key: "some_key")
}
}
}Borrar la misma entrada meta de varios 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")
}
}
}Establecer varias entradas meta a la vez
Puedes establecer varias entradas meta a la vez pasando un JSON a las distintas mutaciones 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"])
}
}
}Establecer entradas meta al crear/actualizar una entidad
Puedes definir entradas meta directamente al crear o actualizar un custom post, etiqueta, categoría o comentario, mediante el parámetro meta.
Esta consulta establece meta al añadir un comentario:
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 consulta inyecta el meta en la mutación anidada 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")
}
}
}
}