プラグインデータのクエリ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")
}
}
}Prev