Wartości meta
Przeczytaj więcej w przewodniku Praca z wartościami meta.
To są przykłady queries do pobierania metadanych i filtrowania wyników po meta.
Pobieranie meta
Pobierz pojedynczą wartość meta _thumbnail_id z postów:
{
posts {
id
title
metaValue(key: "_thumbnail_id")
}
}Pobierz tablicową wartość meta upvotes z komentarzy:
{
comments {
id
content
upvotes: metaValues(key: "upvotes")
}
}Filtrowanie po meta
Filtruj posty, w których klucz meta _thumbnail_id istnieje:
{
posts(filter: {
metaQuery: {
key: "_thumbnail_id",
compareBy:{
key: {
operator: EXISTS
}
}
}
}) {
id
title
metaValue(key: "_thumbnail_id")
}
}Filtruj użytkowników, w których wpis meta nickname ma określoną wartość:
{
users(filter: {
metaQuery: {
key: "nickname",
compareBy:{
stringValue: {
value: "leo"
operator: EQUALS
}
}
}
}) {
id
name
metaValue(key: "nickname")
}
}Filtruj komentarze, w których wpis meta upvotes (który jest tablicą liczb całkowitych) ma wartość 4 lub 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")
}
}Dodawanie meta
Możesz dodawać wpisy meta do custom postów, tagów, kategorii, komentarzy i użytkowników.
Ta query dodaje wpis meta do posta z 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")
}
}
}Ta query dodaje ten sam klucz meta z różnymi wartościami do różnych postów, w trybie zbiorczym:
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")
}
}
}Aktualizowanie meta
Zaktualizuj wpis meta kategorii:
mutation {
updateCategoryMeta(input: {
id: 20
key: "_source"
value: "Updated source value"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
category {
__typename
id
metaValue(key: "_source")
}
}
}Ta query używa zagnieżdżonych mutations do zaktualizowania wartości meta w poście:
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")
}
}
}
}Usuwanie meta
Usuń wpis meta z posta:
mutation {
deletePostMeta(input: {
id: 5
key: "some_key"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
metaValue(key: "some_key")
}
}
}Usuń ten sam wpis meta z wielu postów:
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")
}
}
}Ustawianie wielu wpisów meta naraz
Możesz ustawić wiele wpisów meta naraz, przekazując JSON do różnych 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"])
}
}
}Ustawianie wpisów meta podczas tworzenia/aktualizowania encji
Możesz definiować wpisy meta bezpośrednio podczas tworzenia lub aktualizowania custom posta, tagu, kategorii lub komentarza, za pomocą parametru meta.
Ta query ustawia meta podczas dodawania komentarza:
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"])
}
}
}Ta query wstrzykuje meta w zagnieżdżonej mutation 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")
}
}
}
}