Truy vấn dữ liệu Plugin
Truy vấn dữ liệu PluginYoast SEO

Yoast SEO

Các ví dụ về queries để tương tác với dữ liệu từ plugin Yoast SEO.

Lấy siêu dữ liệu SEO

Chúng ta có thể sử dụng các trường meta để truy vấn siêu dữ liệu SEO:

query GetPost($postId: ID!) {
  post(by: { id: $postId }) {
    id
    title
 
    metaTitle: metaValue(key: "_yoast_wpseo_title")
    metaDesc: metaValue(key: "_yoast_wpseo_metadesc")
    focusKeyword: metaValue(key: "_yoast_wpseo_focuskw")
    socialFBTitle: metaValue(key: "_yoast_wpseo_opengraph-title")
    socialFBDesc: metaValue(key: "_yoast_wpseo_opengraph-description")
    socialFBImage: metaValue(key: "_yoast_wpseo_opengraph-image")
    socialTwitterTitle: metaValue(key: "_yoast_wpseo_twitter-title")
    socialTwitterDesc: metaValue(key: "_yoast_wpseo_twitter-description")
    socialTwitterImage: metaValue(key: "_yoast_wpseo_twitter-image")
  }
}

Cập nhật siêu dữ liệu SEO

Chúng ta có thể sử dụng các meta mutations để cập nhật siêu dữ liệu SEO:

mutation UpdatePost($postId: ID!) {
  updateCustomPostMetas(inputs: [
    { id: $postId, key: "_yoast_wpseo_title", value: "New title" },
    { id: $postId, key: "_yoast_wpseo_metadesc", value: "New description" },
    { id: $postId, key: "_yoast_wpseo_focuskw", value: "New focus keyword" },
    { id: $postId, key: "_yoast_wpseo_opengraph-title", value: "Social title" },
    { id: $postId, key: "_yoast_wpseo_opengraph-description", value: "Social description" },
    { id: $postId, key: "_yoast_wpseo_opengraph-image", value: "https://example.com/social-image.jpg" },
    { id: $postId, key: "_yoast_wpseo_twitter-title", value: "Social title" },
    { id: $postId, key: "_yoast_wpseo_twitter-description", value: "Social description" },
    { id: $postId, key: "_yoast_wpseo_twitter-image", value: "https://example.com/social-image.jpg" },
  ]) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      __typename
      id
      metaTitle: metaValue(key: "_yoast_wpseo_title")
      metaDesc: metaValue(key: "_yoast_wpseo_metadesc")
      focusKeyword: metaValue(key: "_yoast_wpseo_focuskw")
      socialFBTitle: metaValue(key: "_yoast_wpseo_opengraph-title")
      socialFBDesc: metaValue(key: "_yoast_wpseo_opengraph-description")
      socialFBImage: metaValue(key: "_yoast_wpseo_opengraph-image")
      socialTwitterTitle: metaValue(key: "_yoast_wpseo_twitter-title")
      socialTwitterDesc: metaValue(key: "_yoast_wpseo_twitter-description")
      socialTwitterImage: metaValue(key: "_yoast_wpseo_twitter-image")
    }
  }
}