ค่า Meta
อ่านเพิ่มเติมในคู่มือ การทำงานกับค่า Meta
ต่อไปนี้คือตัวอย่าง queries สำหรับดึงข้อมูล meta และกรองผลลัพธ์ด้วย meta
การ Query meta
ดึงค่า meta เดี่ยว _thumbnail_id จากโพสต์:
{
posts {
id
title
metaValue(key: "_thumbnail_id")
}
}ดึงค่า meta แบบอาร์เรย์ upvotes จากคอมเมนต์:
{
comments {
id
content
upvotes: metaValues(key: "upvotes")
}
}การกรองด้วย meta
กรองโพสต์ที่มี meta key _thumbnail_id อยู่:
{
posts(filter: {
metaQuery: {
key: "_thumbnail_id",
compareBy:{
key: {
operator: EXISTS
}
}
}
}) {
id
title
metaValue(key: "_thumbnail_id")
}
}กรองผู้ใช้ที่มี meta entry nickname มีค่าที่กำหนด:
{
users(filter: {
metaQuery: {
key: "nickname",
compareBy:{
stringValue: {
value: "leo"
operator: EQUALS
}
}
}
}) {
id
name
metaValue(key: "nickname")
}
}กรองคอมเมนต์ที่มี meta entry upvotes (ซึ่งเป็นอาร์เรย์ของจำนวนเต็ม) มีค่าเป็น 4 หรือ 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")
}
}การเพิ่ม meta
คุณสามารถเพิ่ม meta entries ให้กับ custom posts, tags, categories, comments และ users ได้
query นี้เพิ่ม meta entry ให้กับโพสต์ที่มี 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")
}
}
}query นี้เพิ่ม meta key เดียวกันพร้อมค่าต่างกันให้กับโพสต์หลายรายการพร้อมกัน:
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")
}
}
}การอัปเดต meta
อัปเดต meta entry ของ category:
mutation {
updateCategoryMeta(input: {
id: 20
key: "_source"
value: "Updated source value"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
category {
__typename
id
metaValue(key: "_source")
}
}
}query นี้ใช้ nested mutations เพื่ออัปเดตค่า meta ในโพสต์:
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")
}
}
}
}การลบ meta
ลบ meta entry จากโพสต์:
mutation {
deletePostMeta(input: {
id: 5
key: "some_key"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
metaValue(key: "some_key")
}
}
}ลบ meta entry เดียวกันจากโพสต์หลายรายการ:
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")
}
}
}การตั้งค่า meta entries หลายรายการพร้อมกัน
คุณสามารถตั้งค่า meta entries หลายรายการพร้อมกันได้โดยส่ง JSON ไปยัง 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"])
}
}
}การตั้งค่า meta entries ขณะสร้าง/อัปเดต entity
คุณสามารถกำหนด meta entries โดยตรงขณะสร้างหรืออัปเดต custom post, tag, category หรือ comment ผ่านพารามิเตอร์ meta
query นี้ตั้งค่า meta ขณะเพิ่มคอมเมนต์:
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"])
}
}
}query นี้ส่ง meta เข้าไปใน nested 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")
}
}
}
}