Query Plugin DataMeta Box
Meta Box
Examples of queries to interact with data from the Meta Box plugin.
Fetching Meta Box custom fields
We can use meta fields to query Meta Box custom fields data, for whatever type they are:
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")
}
}
If the meta value is a relationship (eg: a post, a user, a taxonomy, etc), we can use the value to query the corresponding entity of type Post
, User
, Taxonomy
, etc:
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
}
}
Updating Meta Box custom fields
We can use meta mutations to update Meta Box custom fields data, by passing their field names and values, for whatever type they are:
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