การ Query ข้อมูลปลั๊กอิน
การ Query ข้อมูลปลั๊กอินMeta Box

Meta Box

ตัวอย่าง queries สำหรับโต้ตอบกับข้อมูลจากปลั๊กอิน Meta Box

การดึง Custom Fields ของ Meta Box

เราสามารถใช้ meta fields เพื่อ query ข้อมูล custom fields ของ Meta Box ได้ ไม่ว่าจะเป็นประเภทใดก็ตาม:

query GetPost($postId: ID!) {
  post(by: { id: $postId }) {
    id
    title
 
    text: metaValue(key: "text_field")
    textarea: metaValue(key: "textarea_field")
    select: metaValue(key: "select_field")
    multiSelect: metaValues(key: "multi_select_field")
  }
}

หาก meta value เป็น relationship (เช่น โพสต์ ผู้ใช้ 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: metaValues(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
  }
}

การอัปเดต Custom Fields ของ Meta Box

เราสามารถใช้ meta mutations เพื่ออัปเดตข้อมูล custom fields ของ Meta Box โดยส่งชื่อ field และค่าที่ต้องการ ไม่ว่าจะเป็นประเภทใดก็ตาม:

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"],
      }
    }
  ) {
    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")
    }
  }
}