WordPress 데이터 쿼리
WordPress 데이터 쿼리메타 값

메타 값

가이드 메타 값 다루기에서 자세히 알아보세요.

다음은 메타 데이터를 가져오고 메타로 결과를 필터링하는 쿼리 예시입니다.

메타 쿼리하기

게시글에서 단일 메타 값 _thumbnail_id 가져오기:

{
  posts {
    id
    title
    metaValue(key: "_thumbnail_id")
  }
}

댓글에서 배열 메타 값 upvotes 가져오기:

{
  comments {
    id
    content
    upvotes: metaValues(key: "upvotes")
  }
}

메타로 필터링하기

메타 키 _thumbnail_id 가 존재하는 게시글 필터링:

{
  posts(filter: {
    metaQuery: {
      key: "_thumbnail_id",
      compareBy:{
        key: {
          operator: EXISTS
        }
      }
    }
  }) {
    id
    title
    metaValue(key: "_thumbnail_id")
  }
}

메타 항목 nickname 이 특정 값을 가진 사용자 필터링:

{
  users(filter: {
    metaQuery: {
      key: "nickname",
      compareBy:{
        stringValue: {
          value: "leo"
          operator: EQUALS
        }
      }
    }
  }) {
    id
    name
    metaValue(key: "nickname")
  }
}

메타 항목 upvotes(정수 배열)가 4 또는 5 값을 가진 댓글 필터링:

{
  comments(filter: {
    metaQuery: [
      {
        relation: OR
        key: "upvotes",
        compareBy: {
          arrayValue: {
            value: 4
            operator: IN
          }
        }
      },
      {
        key: "upvotes",
        compareBy: {
          arrayValue: {
            value: 5
            operator: IN
          }
        }
      }
  ]}) {
    id
    content
    upvotes: metaValues(key: "upvotes")
  }
}

메타 추가하기

커스텀 게시글, 태그, 카테고리, 댓글, 사용자에 메타 항목을 추가할 수 있습니다.

이 쿼리는 ID 4 의 게시글에 메타 항목을 추가합니다:

mutation {
  addCustomPostMeta(input: {
    id: 4
    key: "some_key"
    value: "Some value"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      id
      metaValue(key: "some_key") 
    }
  }
}

이 쿼리는 동일한 메타 키에 서로 다른 값을 여러 게시글에 일괄 추가합니다:

mutation {
  addCustomPostMetas(inputs: [
    {
      id: 4
      key: "some_key"
      value: "Some value"
    },
    {
      id: 5
      key: "some_key"
      value: "Some other value"
    },
    {
      id: 6
      key: "some_key"
      value: "Yet another value"
    }
  ]) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      id
      metaValue(key: "some_key") 
    }
  }
}

메타 업데이트하기

카테고리 메타 항목 업데이트:

mutation {
  updateCategoryMeta(input: {
    id: 20
    key: "_source"
    value: "Updated source value"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      __typename
      id
      metaValue(key: "_source") 
    }
  }
}

이 쿼리는 중첩 뮤테이션을 사용하여 게시글의 메타 값을 업데이트합니다:

mutation {
  post(by: {id: 1}) {
    updateMeta(input: {
      key: "some_key"
      value: "Updated description"
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        metaValue(key: "single_meta_key") 
      }
    }
  }
}

메타 삭제하기

게시글에서 메타 항목 삭제:

mutation {
  deletePostMeta(input: {
    id: 5
    key: "some_key"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      metaValue(key: "some_key") 
    }
  }
}

여러 게시글에서 동일한 메타 항목 삭제:

mutation {
  deletePostMetas(inputs: [
    {
      id: 5
      key: "some_key"
    },
    {
      id: 6
      key: "some_key"
    }
  ]) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      metaValue(key: "some_key") 
    }
  }
}

여러 메타 항목을 한 번에 설정하기

JSON을 각 set{Entity}Meta 뮤테이션에 전달하여 여러 메타 항목을 한 번에 설정할 수 있습니다:

mutation {
  setCustomPostMeta(input: {
    id: 4
    entries: {
      single_meta_key: [
        "This is a single entry",
      ],
      object_meta_key: [
        {
          key: "This is a key",
          value: "This is a value",
        },
      ],
      array_meta_key: [
        "This is a string",
        "This is another string",
      ],
      object_array_meta_key: [
        [
          {
            key: "This is a key 1",
            value: "This is a value 1",
          },
          {
            key: "This is a key 2",
            value: "This is a value 2",
          },
        ]
      ],
    }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      id
      meta(keys: ["single_meta_key", "object_meta_key", "array_meta_key", "object_array_meta_key"])
    }
  }
}

엔티티 생성·업데이트 시 메타 항목 설정하기

커스텀 게시글, 태그, 카테고리, 또는 댓글을 생성하거나 업데이트할 때 파라미터 meta 를 통해 메타 항목을 직접 정의할 수 있습니다.

이 쿼리는 댓글을 추가할 때 메타를 설정합니다:

mutation {
  addCommentToCustomPost(input: {
    customPostID: 1130
    commentAs: { html: "New comment" }
    meta: {
      some_meta_key: [
        "This is a single entry",
      ],
      another_meta_key: [
        "This is an array entry 1",
        "This is an array entry 2",
      ],
    }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    comment {
      id
      meta(keys: ["some_meta_key", "another_meta_key"]) 
    }
  }
}

이 쿼리는 중첩 뮤테이션 Post.update 에 메타를 주입합니다:

mutation {
  post(by: {id: 1}) {
    update(input: {
      meta: {
        single_meta_key: [
          "This is an updated value",
        ]
      }
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      post {
        id
        metaValue(key: "single_meta_key") 
      }
    }
  }
}