Truy vấn dữ liệu WordPress
Truy vấn dữ liệu WordPressDanh mục tùy chỉnh

Danh mục tùy chỉnh

Đọc thêm trong hướng dẫn Làm việc với danh mục tùy chỉnh.

Đây là các ví dụ về queries để lấy dữ liệu phân loại danh mục tùy chỉnh.

Các phân loại chưa được ánh xạ vào schema

Lấy các danh mục với phân loại "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
        }
      }
    }
  }
}

Lấy các danh mục liên kết với một custom post

Kiểu GenericCustomPost có trường categories, để lấy các danh mục tùy chỉnh đã được thêm vào custom post:

query {
  customPosts(
    filter: { customPostTypes: "product" }
  ) {
    __typename
 
    ... on GenericCustomPost {
      categories(taxonomy: "product-cat") {
        __typename
        id
        name
        taxonomy
      }
    }
  }
}

Lọc custom post theo danh mục

Để lấy các custom post với các danh mục nhất định, chúng ta có thể sử dụng input filter.categories:

query {
  customPostsByCatIDs: customPosts(
    filter: {
      categories: {
        includeBy: {
          ids: [26, 28]
        }
        taxonomy: "product-cat"
      }
    }
  ) {
    id
    title
  }
 
  customPostsByCatSlugs: customPosts(
    filter: {
      categories: {
        includeBy: {
          slugs: ["news", "sports"]
        }
        taxonomy: "product-cat"
      }
    }
  ) {
    id
    title
  }
}

Thiết lập danh mục cho một custom post

Mutation:

mutation {
  setCategoriesOnCustomPost(
    input: {
      id: 1499, 
      categoryIDs: [2, 5]
      taxonomy: "cat-taxonomy"
    }
  ) {
    status
    errors {
      __typename
      ... on ErrorPayload {
        message
      }
    }
    customPostID
    customPost {
      categories(taxonomy: "cat-taxonomy") {
        id
      }
      categoryNames(taxonomy: "cat-taxonomy")
    }
  }
}

Mutation lồng nhau:

mutation {
  customPost(by: { id: 1499 }) {
    setCategories(
      input: {
        categoryIDs: [2, 5]
        taxonomy: "cat-taxonomy"
      }
    ) {
      status
      errors {
        __typename
        ... on ErrorPayload {
          message
        }
      }
      customPostID
      customPost {
        categories(taxonomy: "cat-taxonomy") {
          id
        }
        categoryNames(taxonomy: "cat-taxonomy")
      }
    }
  }
}

Tạo, cập nhật và xóa một danh mục tùy chỉnh

Query này tạo, cập nhật và xóa các thuật ngữ danh mục cho danh mục tùy chỉnh some-cat-taxonomy:

mutation CreateUpdateDeleteCategories {
  createCategory(input: {
    taxonomy: "some-cat-taxonomy",
    name: "Some name"
    slug: "Some slug"
    description: "Some description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...CategoryData
    }
  }
 
  updateCategory(input: {
    id: 1
    taxonomy: "some-cat-taxonomy"
    name: "Some updated name"
    slug: "Some updated slug"
    description: "Some updated description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...CategoryData
    }
  }
 
  deleteCategory(input: {
    id: 1
    taxonomy: "some-cat-taxonomy"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
}
 
fragment CategoryData on Category {
  id
  name
  slug
  description
  parent {
    id
  }
}