WordPressデータのクエリ
WordPressデータのクエリページ

ページ

以下は、ページデータを取得するクエリの例です。

ページの取得

単一のページ:

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

このクエリはネストされた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
      }
    }
  }
}