หมวดหมู่แบบกำหนดเอง
ใน WordPress เราสามารถเพิ่มหมวดหมู่ให้กับโพสต์ได้ (กล่าวคือ โดยใช้ taxonomy ที่มีชื่อว่า "category") ซึ่งได้ถูก map ไว้ใน GraphQL schema แล้วผ่านประเภท PostCategory โดยเชื่อมโยงกับ entry ประเภท Post
ในทำนองเดียวกัน custom post type ที่กำหนดโดย theme หรือ plugin (เช่น "product") สามารถมี taxonomy หมวดหมู่ของตัวเองที่เชื่อมโยงอยู่ได้ (เช่น "product-cat") เนื่องจาก custom post type เหล่านี้ไม่ได้ถูก map ไว้ใน GraphQL schema จึงถูก resolve ผ่านประเภท GenericCustomPost และหมวดหมู่ของ custom post type เหล่านั้นจะถูก resolve เป็น GenericCategory
เราใช้ฟิลด์ category และ categories เพื่อดึงข้อมูลหมวดหมู่ โดยระบุว่าอ้างอิง taxonomy ใดผ่าน field argument taxonomy ผลลัพธ์จะเป็น union type CategoryUnion ซึ่งประกอบด้วย entry จาก PostCategory หรือ GenericCategory (ขึ้นอยู่กับ taxonomy ของ entry นั้น)
ตัวอย่างเช่น queries นี้จะดึงหมวดหมู่ที่มี taxonomy เป็น "product-category":
query {
categories(taxonomy: "product-category") {
__typename
...on Category {
count
description
id
name
slug
url
}
...on GenericCategory {
taxonomy
customPostCount
customPosts {
__typename
...on CustomPost {
id
title
}
}
}
}
}การอนุญาตให้เข้าถึง category taxonomy ที่ยังไม่ได้ map
category taxonomy ที่เข้าถึงได้ผ่านประเภท GenericCategory จะต้องได้รับการตั้งค่าอย่างชัดเจนในหน้า Settings ของ plugin ตามที่อธิบายไว้ในคู่มือ การเพิ่ม custom category taxonomy เข้าสู่ schema