플러그인 데이터 쿼리
플러그인 데이터 쿼리Meta Box

Meta Box

Meta Box 플러그인의 데이터를 조작하는 쿼리 예시입니다.

Meta Box 커스텀 필드 가져오기

메타 필드를 사용하여 Meta Box 커스텀 필드 데이터를 타입에 관계없이 쿼리할 수 있습니다.

query GetPost($postId: ID!) {
  post(by: { id: $postId }) {
    id
    title
 
    text: metaValue(key: "text_field")
    textarea: metaValue(key: "textarea_field")
    select: metaValue(key: "select_field")
    multiSelect: metaValues(key: "multi_select_field")
  }
}

메타 값이 관계(예: 포스트, 사용자, 택소노미 등)인 경우, 해당 값을 사용하여 Post, User, Taxonomy 등의 타입에 해당하는 엔티티를 쿼리할 수 있습니다.

query GetPostWithRelationships($postId: ID!) {
  post(by: { id: $postId }) {
    id
    title
    
    # Export the relationship to a post
    relationshipPostId: metaValue(key: "relationship_post_id")
      @export(as: "relationshipPostId")
 
    # Export the relationship to a list of posts
    relationshipPostIds: metaValues(key: "relationship_post_ids")
      @export(as: "relationshipPostIds")
  }
}
 
query QueryPostRelationships @depends(on: "GetPostWithRelationships") {  
  # Query the relationship to a post
  relationshipPost: post(by: { id: $relationshipPostId }) {
    id
    title
  }
 
  # Query the relationship to a list of posts
  relationshipPosts: posts(filter: { ids: $relationshipPostIds }) {
    id
    title
  }
}

Meta Box 커스텀 필드 업데이트

메타 뮤테이션을 사용하여 필드 이름과 값을 전달함으로써 Meta Box 커스텀 필드 데이터를 타입에 관계없이 업데이트할 수 있습니다.

mutation UpdatePost($postId: ID!) {
  updatePost(
    input: {
      id: $postId
      meta: {
        text_field: ["New text value"],
        textarea_field: ["New textarea value"],
        select_field: ["New select value"],
        multi_select_field: ["Choice 1", "Choice 2"],
      }
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      text: metaValue(key: "text_field")
      textarea: metaValue(key: "textarea_field")
      select: metaValue(key: "select_field")
      multiSelect: metaValues(key: "multi_select_field")
    }
  }
}