Query WordPress 数据
Query WordPress 数据元数据值

元数据值

在指南 元数据值的操作 中可了解更多详情。

以下是获取元数据及按元数据筛选结果的 Query 示例。

查询元数据

从文章中获取单一元数据值 _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(整数数组)值为 45 的评论:

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

添加元数据

您可以向自定义文章、标签、分类、评论和用户添加元数据条目。

以下 Query 向 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") 
    }
  }
}

以下 Query 批量为多篇文章添加相同元数据键但不同值的条目:

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

以下 Query 使用嵌套 mutation 更新文章中的元数据值:

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

一次设置多个元数据条目

通过向各 set{Entity}Meta mutation 传入 JSON,可以一次设置多个元数据条目:

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 直接定义元数据条目。

以下 Query 在添加评论时设置元数据:

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

以下 Query 在嵌套 mutation 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") 
      }
    }
  }
}
Prev
Next