WordPress 데이터 쿼리
WordPress 데이터 쿼리포스트 카테고리

포스트 카테고리

포스트 카테고리 데이터를 가져오는 쿼리 예시입니다.

카테고리 가져오기

포스트 카테고리 목록을 이름순으로 정렬하고 포스트 수를 표시합니다:

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

포스트의 모든 카테고리:

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

포스트의 카테고리 이름:

query {
  posts {
    id
    title
    categoryNames
  }
}

미리 지정된 카테고리 목록:

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

이름으로 카테고리 필터링:

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

카테고리 결과 수 집계:

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

카테고리 페이지네이션:

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

최상위 카테고리만, 그리고 2번째 레벨의 하위 카테고리:

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

메타 값 가져오기:

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

포스트에 카테고리 설정하기

Mutation:

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

중첩 mutation:

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

포스트 카테고리 생성, 수정 및 삭제

이 쿼리는 포스트 카테고리 텀(term)을 생성, 수정 및 삭제합니다:

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
  }
}