Truy vấn dữ liệu WordPress
Truy vấn dữ liệu WordPressGiá trị Meta

Giá trị Meta

Đọc thêm trong hướng dẫn Làm việc với giá trị Meta.

Đây là các ví dụ về queries để lấy dữ liệu meta và lọc kết quả theo meta.

Truy vấn meta

Lấy giá trị meta đơn _thumbnail_id từ các bài viết:

{
  posts {
    id
    title
    metaValue(key: "_thumbnail_id")
  }
}

Lấy giá trị meta dạng mảng upvotes từ các bình luận:

{
  comments {
    id
    content
    upvotes: metaValues(key: "upvotes")
  }
}

Lọc theo meta

Lọc các bài viết có khóa meta _thumbnail_id tồn tại:

{
  posts(filter: {
    metaQuery: {
      key: "_thumbnail_id",
      compareBy:{
        key: {
          operator: EXISTS
        }
      }
    }
  }) {
    id
    title
    metaValue(key: "_thumbnail_id")
  }
}

Lọc người dùng có mục meta nickname mang một giá trị nhất định:

{
  users(filter: {
    metaQuery: {
      key: "nickname",
      compareBy:{
        stringValue: {
          value: "leo"
          operator: EQUALS
        }
      }
    }
  }) {
    id
    name
    metaValue(key: "nickname")
  }
}

Lọc các bình luận có mục meta upvotes (là một mảng số nguyên) có giá trị 4 hoặc 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")
  }
}

Thêm meta

Bạn có thể thêm mục meta vào custom post, tag, danh mục, bình luận và người dùng.

Query này thêm một mục meta vào bài viết có 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 này thêm cùng một khóa meta với các giá trị khác nhau vào nhiều bài viết khác nhau, theo lô:

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

Cập nhật meta

Cập nhật một mục meta của danh mục:

mutation {
  updateCategoryMeta(input: {
    id: 20
    key: "_source"
    value: "Updated source value"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    category {
      __typename
      id
      metaValue(key: "_source") 
    }
  }
}

Query này sử dụng các mutation lồng nhau để cập nhật giá trị meta trong một bài viết:

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

Xóa meta

Xóa một mục meta khỏi bài viết:

mutation {
  deletePostMeta(input: {
    id: 5
    key: "some_key"
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    post {
      id
      metaValue(key: "some_key") 
    }
  }
}

Xóa cùng một mục meta khỏi nhiều bài viết:

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

Đặt nhiều mục meta cùng một lúc

Bạn có thể đặt nhiều mục meta cùng một lúc bằng cách truyền JSON vào các mutation set{Entity}Meta khác nhau:

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

Đặt mục meta khi tạo/cập nhật một thực thể

Bạn có thể định nghĩa các mục meta trực tiếp khi tạo hoặc cập nhật một custom post, tag, danh mục hoặc bình luận, thông qua tham số meta.

Query này đặt meta khi thêm một bình luận:

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 này đưa meta vào mutation lồng nhau 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