Truy vấn dữ liệu WordPress
Truy vấn dữ liệu WordPressTrang

Trang

Đây là các ví dụ về queries để lấy dữ liệu trang.

Lấy dữ liệu trang

Một trang đơn lẻ:

query {
  page(by: { id: 2 }) {
    id
    title
    content
    url
    date
  }
}

Danh sách các trang:

query {
  pages(pagination: { limit: 5 }) {
    id
    title
    excerpt
    url
    dateStr(format: "d/m/Y")
  }
}

Các trang cấp cao nhất cùng với các trang con của chúng:

query {
  pages(filter: { parentID: 0 }) {
    ...PageProps
    children {
      ...PageProps
      children(pagination: { limit: 3 }) {
        ...PageProps
      }
    }
  }
}
 
fragment PageProps on Page {
  id
  title
  date
  urlPath
}

Lấy dữ liệu trang của người dùng đã đăng nhập

Các trường page, pagespageCount chỉ lấy các trang có trạng thái "publish".

Để lấy các trang của người dùng đã đăng nhập với bất kỳ trạng thái nào ("publish", "pending", "draft" hoặc "trash"), hãy sử dụng các trường sau:

  • myPage
  • myPages
  • myPageCount
query {
  myPages(filter: { status: [draft, pending] }) {
    id
    title
    status
  }
}

Tạo trang

Chỉ người dùng đã đăng nhập mới có thể tạo trang.

mutation {
  createPage(
    input: {
      title: "Hi there!"
      contentAs: { html: "How do you like it?" }
      status: draft
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
      ...on GenericErrorPayload {
        code
      }
    }
    pageID
    page {
      status
      title
      content
      url
      date
      author {
        id
        name
      }
    }
  }
}

Cập nhật trang

Chỉ những người dùng có quyền hạn tương ứng mới có thể chỉnh sửa trang.

mutation {
  updatePage(
    input: {
      id: 2,
      title: "This is my new title",
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
      ...on GenericErrorPayload {
        code
      }
    }
    page {
      id
      title
    }
  }
}

Query này sử dụng nested mutations để cập nhật trang:

mutation {
  page(by: { id: 2 }) {
    originalTitle: title
    update(input: {
      title: "This is my new title",
      contentAs: { html: "This rocks!" }
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      page {
        newTitle: title
        content
      }
    }
  }
}