การ Query ข้อมูลปลั๊กอินAdvanced Custom Fields (ACF)
Advanced Custom Fields (ACF)
อ่านเพิ่มเติมในคู่มือ Working with Advanced Custom Fields (ACF)
ตัวอย่าง queries สำหรับโต้ตอบกับข้อมูลจากปลั๊กอิน Advanced Custom Fields (ACF)
การดึงข้อมูล ACF custom fields
เราสามารถใช้ meta fields เพื่อ query ข้อมูล ACF custom fields ได้ ไม่ว่าจะเป็นประเภทใดก็ตาม:
query GetPost($postId: ID!) {
post(by: { id: $postId }) {
id
title
# Basic field types
text: metaValue(key: "text_field")
textarea: metaValue(key: "textarea_field")
select: metaValue(key: "select_field")
multiSelect: metaValue(key: "multi_select_field")
number: metaValue(key: "number_field")
# Date field
dateAsString: metaValue(key: "date_field")
dateYear: _strSubstr(string: $__dateAsString, offset: 0, length: 4)
dateMonth: _strSubstr(string: $__dateAsString, offset: 4, length: 2)
dateDay: _strSubstr(string: $__dateAsString, offset: 6, length: 2)
dateTime: _makeTime(year: $__dateYear, month: $__dateMonth, day: $__dateDay, hour: 0, minute: 0, second: 0)
date: _date(format: "Y-m-d", timestamp: $__dateTime)
}
}หาก meta value เป็นความสัมพันธ์ (เช่น โพสต์ ผู้ใช้ taxonomy เป็นต้น) เราสามารถใช้ค่านั้นเพื่อ query entity ที่สอดคล้องกันในประเภท Post, User, Taxonomy เป็นต้น:
query GetPostWithRelationships($postId: ID!) {
post(by: { id: $postId }) {
id
title
# Export the relationship to a post
relationshipPostId: metaValue(key: "relationship_post_id")
@export(as: "relationshipPostId")
# Export the relationship to a list of posts
relationshipPostIds: metaValue(key: "relationship_post_ids")
@export(as: "relationshipPostIds")
}
}
query QueryPostRelationships @depends(on: "GetPostWithRelationships") {
# Query the relationship to a post
relationshipPost: post(by: { id: $relationshipPostId }) {
id
title
}
# Query the relationship to a list of posts
relationshipPosts: posts(filter: { ids: $relationshipPostIds }) {
id
title
}
}การอัปเดต ACF custom fields
เราสามารถใช้ meta mutations เพื่ออัปเดตข้อมูล ACF custom fields โดยส่งชื่อฟิลด์และค่าต่างๆ ได้ ไม่ว่าจะเป็นประเภทใดก็ตาม:
mutation UpdatePost($postId: ID!) {
updatePost(
input: {
id: $postId
meta: {
text_field: ["New text value"],
textarea_field: ["New textarea value"],
select_field: ["New select value"],
multi_select_field: ["Choice 1", "Choice 2"],
number_field: [42],
date_field: ["20240320"],
}
}
) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
text: metaValue(key: "text_field")
textarea: metaValue(key: "textarea_field")
select: metaValue(key: "select_field")
multiSelect: metaValues(key: "multi_select_field")
number: metaValue(key: "number_field")
date: metaValue(key: "date_field")
}
}
}Prev