Query WordPress 数据
Query WordPress 数据页面

页面

以下是获取页面数据的 Query 示例。

获取页面

单个页面:

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

页面列表:

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

顶级页面及其子页面:

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

获取已登录用户的页面

字段 pagepagespageCount 仅获取状态为 "publish" 的页面。

若要获取已登录用户的页面,并支持任意状态("publish""pending""draft""trash"),请使用以下字段:

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

创建页面

只有已登录的用户才能创建页面。

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

更新页面

只有拥有相应权限的用户才能编辑页面。

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

此 Query 使用嵌套 mutation 来更新页面:

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
      }
    }
  }
}
Prev
Next