Zapytania o dane wtyczekAdvanced Custom Fields (ACF)
Advanced Custom Fields (ACF)
Dowiedz się więcej w przewodniku Praca z Advanced Custom Fields (ACF).
Przykłady queries do interakcji z danymi wtyczki Advanced Custom Fields (ACF).
Pobieranie niestandardowych pól ACF
Możemy używać pól meta do odpytywania danych niestandardowych pól ACF, niezależnie od ich typu:
query GetPost($postId: ID!) {
post(by: { id: $postId }) {
id
title
# Podstawowe typy pól
text: metaValue(key: "text_field")
textarea: metaValue(key: "textarea_field")
select: metaValue(key: "select_field")
multiSelect: metaValue(key: "multi_select_field")
number: metaValue(key: "number_field")
# Pole daty
dateAsString: metaValue(key: "date_field")
dateYear: _strSubstr(string: $__dateAsString, offset: 0, length: 4)
dateMonth: _strSubstr(string: $__dateAsString, offset: 4, length: 2)
dateDay: _strSubstr(string: $__dateAsString, offset: 6, length: 2)
dateTime: _makeTime(year: $__dateYear, month: $__dateMonth, day: $__dateDay, hour: 0, minute: 0, second: 0)
date: _date(format: "Y-m-d", timestamp: $__dateTime)
}
}Jeśli wartość meta jest relacją (np. post, użytkownik, taksonomia itp.), możemy użyć tej wartości do odpytania odpowiedniej encji typu Post, User, Taxonomy itp.:
query GetPostWithRelationships($postId: ID!) {
post(by: { id: $postId }) {
id
title
# Eksportuj relację z postem
relationshipPostId: metaValue(key: "relationship_post_id")
@export(as: "relationshipPostId")
# Eksportuj relację z listą postów
relationshipPostIds: metaValue(key: "relationship_post_ids")
@export(as: "relationshipPostIds")
}
}
query QueryPostRelationships @depends(on: "GetPostWithRelationships") {
# Odpytaj relację z postem
relationshipPost: post(by: { id: $relationshipPostId }) {
id
title
}
# Odpytaj relację z listą postów
relationshipPosts: posts(filter: { ids: $relationshipPostIds }) {
id
title
}
}Aktualizowanie niestandardowych pól ACF
Możemy używać mutations meta do aktualizowania danych niestandardowych pól ACF, podając nazwy i wartości pól, niezależnie od ich typu:
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"],
number_field: [42],
date_field: ["20240320"],
}
}
) {
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")
number: metaValue(key: "number_field")
date: metaValue(key: "date_field")
}
}
}Prev