Plugin-Daten abfragenMeta Box
Meta Box
Beispiele für queries zur Interaktion mit Daten aus dem Meta Box Plugin.
Meta Box benutzerdefinierte Felder abrufen
Du kannst Meta-Felder verwenden, um benutzerdefinierte Felder von Meta Box abzufragen, unabhängig von ihrem Typ:
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")
}
}Wenn der Meta-Wert eine Beziehung ist (z. B. ein Post, ein Benutzer, eine Taxonomie usw.), kannst du den Wert verwenden, um die entsprechende Entität vom Typ Post, User, Taxonomy usw. abzufragen:
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 benutzerdefinierte Felder aktualisieren
Du kannst Meta-Mutationen verwenden, um benutzerdefinierte Felder von Meta Box zu aktualisieren, indem du ihre Feldnamen und Werte übergibst, unabhängig von ihrem Typ:
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