Truy vấn dữ liệu WordPress
Truy vấn dữ liệu WordPressBài viết tùy chỉnh

Bài viết tùy chỉnh

Đọc thêm trong hướng dẫn Làm việc với bài viết tùy chỉnh.

Đây là các ví dụ về queries để lấy dữ liệu bài viết tùy chỉnh.

CPT được ánh xạ vào schema

Lấy các bài viết tùy chỉnh với CPT "post""page":

query {
  customPosts(filter: { customPostTypes: ["post", "page"] }) {
    ...CustomPostProps
    ...PostProps
    ...PageProps
  }
}
 
fragment CustomPostProps on CustomPost {
  __typename
  title
  excerpt
  url
  dateStr(format: "d/m/Y")
}
 
fragment PostProps on Post {
  tags {
    id
    name
  }
}
 
fragment PageProps on Page {
  author {
    id
    name
  }
}

CPT không được ánh xạ vào schema

Lấy các bài viết tùy chỉnh cho nhiều loại CPT khác nhau (phải được bật để có thể truy vấn qua Cài đặt):

query {
  customPosts(
    filter:{
      customPostTypes: [
        "page",
        "nav_menu_item",
        "wp_block",
        "wp_global_styles"
      ]
    }
  ) {
    ... on CustomPost {
      id
      title
      customPostType
      status
    }
    __typename
  }
}

Lọc CPT theo taxonomy tùy chỉnh

Lấy các bài viết tùy chỉnh được lọc theo danh mục:

query {
  customPosts(
    filter: {
      categories: {
        includeBy: {
          ids: [26, 28]
        }
        taxonomy: "product-cat"
      }
    }
  ) {
    ... on CustomPost {
      id
      title
    }
    ... on GenericCustomPost {
      categories(taxonomy: "product-cat") {
        id
      }
    }
  }
}

Tạo bài viết tùy chỉnh

Để tạo các CPT không yêu cầu thêm trường nào ngoài các trường của Post, bạn có thể sử dụng mutation createCustomPost.

Query này tạo một mục cho CPT "my-portfolio":

mutation {
  createCustomPost(
    input: {
      customPostType: "my-portfolio"
      title: "My photograph"
      contentAs: { html: "This is my photo, check it out." }
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
      ...on GenericErrorPayload {
        code
      }
    }
    customPost {
      __typename
      ...on CustomPost {
        id
        title
        content
      }
    }
  }
}

Cập nhật bài viết tùy chỉnh

Query này cập nhật tiêu đề và nội dung cho CPT "my-portfolio":

mutation {
  updateCustomPost(input: {
    id: 1
    customPostType: "my-portfolio"
    title: "Updated title"
    contentAs: { html: "Updated content" }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      __typename
      ...on CustomPost {
        id
        title
        content
      }
    }
  }
}

Hoặc sử dụng mutations lồng nhau:

mutation {
  customPost(by: { id: 1 }, customPostTypes: "my-portfolio") {
    originalTitle: title
    update(input: {
      title: "This is my new title",
      contentAs: { html: "This rocks!" }
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      customPost {
        __typename
        ...on CustomPost {
          id
          newTitle: title
          content
        }
      }
    }
  }
}