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")
}
}
}
}