WordPress 데이터 쿼리
WordPress 데이터 쿼리커스텀 포스트

커스텀 포스트

자세한 내용은 가이드 커스텀 포스트 작업하기를 참조하세요.

다음은 커스텀 포스트 데이터를 가져오는 쿼리 예시입니다.

스키마에 매핑된 CPT

CPT "post""page" 의 커스텀 포스트를 가져옵니다:

query {
  customPosts(filter: { customPostTypes: ["post", "page"] }) {
    ...CustomPostProps
    ...PostProps
    ...PageProps
  }
}
 
fragment CustomPostProps on CustomPost {
  __typename
  title
  excerpt
  url
  dateStr(format: "d/m/Y")
}
 
fragment PostProps on Post {
  tags {
    id
    name
  }
}
 
fragment PageProps on Page {
  author {
    id
    name
  }
}

스키마에 매핑되지 않은 CPT

다양한 CPT(설정에서 쿼리 가능하도록 활성화해야 합니다)의 커스텀 포스트를 가져옵니다:

query {
  customPosts(
    filter:{
      customPostTypes: [
        "page",
        "nav_menu_item",
        "wp_block",
        "wp_global_styles"
      ]
    }
  ) {
    ... on CustomPost {
      id
      title
      customPostType
      status
    }
    __typename
  }
}

커스텀 택소노미로 CPT 필터링

카테고리로 필터링하여 커스텀 포스트를 가져옵니다:

query {
  customPosts(
    filter: {
      categories: {
        includeBy: {
          ids: [26, 28]
        }
        taxonomy: "product-cat"
      }
    }
  ) {
    ... on CustomPost {
      id
      title
    }
    ... on GenericCustomPost {
      categories(taxonomy: "product-cat") {
        id
      }
    }
  }
}

커스텀 포스트 생성

Post의 필드 이외에 추가 필드가 필요하지 않은 CPT를 생성하려면 createCustomPost 뮤테이션을 사용할 수 있습니다.

이 쿼리는 "my-portfolio" CPT에 항목을 생성합니다:

mutation {
  createCustomPost(
    input: {
      customPostType: "my-portfolio"
      title: "My photograph"
      contentAs: { html: "This is my photo, check it out." }
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
      ...on GenericErrorPayload {
        code
      }
    }
    customPost {
      __typename
      ...on CustomPost {
        id
        title
        content
      }
    }
  }
}

커스텀 포스트 업데이트

이 쿼리는 "my-portfolio" CPT의 제목과 콘텐츠를 업데이트합니다:

mutation {
  updateCustomPost(input: {
    id: 1
    customPostType: "my-portfolio"
    title: "Updated title"
    contentAs: { html: "Updated content" }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      __typename
      ...on CustomPost {
        id
        title
        content
      }
    }
  }
}

또는 중첩 뮤테이션을 사용하는 경우:

mutation {
  customPost(by: { id: 1 }, customPostTypes: "my-portfolio") {
    originalTitle: title
    update(input: {
      title: "This is my new title",
      contentAs: { html: "This rocks!" }
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      customPost {
        __typename
        ...on CustomPost {
          id
          newTitle: title
          content
        }
      }
    }
  }
}