Truy vấn dữ liệu WordPress
Truy vấn dữ liệu WordPressDanh Mục Bài Viết

Danh Mục Bài Viết

Đây là các ví dụ về queries để lấy dữ liệu danh mục bài viết.

Lấy danh mục

Danh sách các danh mục bài viết, sắp xếp theo tên và hiển thị số lượng bài viết:

query {
  postCategories(
    sort: { order: ASC, by: NAME }
    pagination: { limit: 50 }
  ) {
    id
    name
    url
    postCount
  }
}

Tất cả danh mục trong một bài viết:

query {
  post(by: { id: 1 }) {
    categories {
      id
      name
      url
    }
  }
}

Tên danh mục trong các bài viết:

query {
  posts {
    id
    title
    categoryNames
  }
}

Danh sách các danh mục được xác định trước:

query {
  postCategories(filter: { ids: [2, 5] }) {
    id
    name
    url
  }
}

Lọc danh mục theo tên:

query {
  postCategories(filter: { search: "rr" }) {
    id
    name
    url
  }
}

Đếm kết quả danh mục:

query {
  postCategoryCount(filter: { search: "rr" })
}

Phân trang danh mục:

query {
  postCategories(
  	pagination: {
  	  limit: 3,
  	  offset: 3
  	}
  ) {
    id
    name
    url
  }
}

Chỉ các danh mục cấp cao nhất và cấp con thứ 2:

{
  postCategories(pagination: { limit: 50 }, filter: { parentID: 0 }) {
    ...CatProps
    children {
      ...CatProps
      children {
        ...CatProps
      }
    }
  }
}
 
fragment CatProps on PostCategory {
  id
  name
  parent {
    id
    name
  }
  childNames
  childCount
}

Lấy các giá trị meta:

query {
  postCategories(
  	pagination: { limit: 5 }
  ) {
    id
    name
    metaValue(
      key: "someKey"
    )
  }
}

Gán danh mục cho bài viết

Mutation:

mutation {
  setCategoriesOnPost(
    input: {
      id: 1499, 
      categoryIDs: [2, 5]
    }
  ) {
    status
    errors {
      __typename
      ... on ErrorPayload {
        message
      }
    }
    postID
    post {
      categories {
        id
      }
      categoryNames
    }
  }
}

Mutation lồng nhau:

mutation {
  post(by: { id: 1499 }) {
    setCategories(
      input: {
        categoryIDs: [2, 5]
      }
    ) {
      status
      errors {
        __typename
        ... on ErrorPayload {
          message
        }
      }
      postID
      post {
        categories {
          id
        }
        categoryNames
      }
    }
  }
}

Tạo, cập nhật và xóa danh mục bài viết

Query này tạo, cập nhật và xóa các term danh mục bài viết:

mutation CreateUpdateDeletePostCategories {
  createPostCategory(input: {
    name: "Some name"
    slug: "Some slug"
    description: "Some description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...PostCategoryData
    }
  }
 
  updatePostCategory(input: {
    id: 1
    name: "Some updated name"
    slug: "Some updated slug"
    description: "Some updated description"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      ...PostCategoryData
    }
  }
 
  deletePostCategory(input: {
    id: 1
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
  }
}
 
fragment PostCategoryData on PostCategory {
  id
  name
  slug
  description
  parent {
    id
  }
}