Query data WordPress
Query data WordPressPostingan

Postingan

Berikut adalah contoh query untuk mengambil dan mengubah data postingan.

Mengambil postingan

Satu postingan, dengan penulis dan tag:

query {
  post(by: { id: 1 }) {
    title
    content
    url
    date
    author {
      id
      name
    }
    tags {
      id
      name
    }
  }
}

Daftar 5 postingan beserta komentarnya:

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

Daftar postingan yang telah ditentukan:

query {
  posts(filter: { ids: [1499, 1657] }) {
    id
    title
    excerpt
    url
    date
  }
}

Memfilter postingan:

query {
  posts(
    filter: { search: "wordpress", dateQuery: { after: "2019-06-01" } },
    sort: { order: ASC, by: TITLE }
  ) {
    id
    title
    excerpt
    url
    status
  }
}

Menghitung hasil postingan:

query {
  postCount(
    filter: { search: "api" }
  )
}

Membuat halaman postingan:

query {
  posts(
    pagination: {
      limit: 5,
      offset: 5
    }
  ) {
    id
    title
  }
}

Postingan dengan tag:

query {
  posts(
    filter: { tagSlugs: ["graphql", "wordpress", "plugin"] }
  ) {
    id
    title
  }
}

Postingan dengan kategori:

query {
  posts(
    filter: { categoryIDs: [50, 190] }
  ) {
    id
    title
  }
}

Mengambil nilai meta:

query {
  posts {
    title
    metaValue(
      key: "_wp_page_template",
    )
  }
}

Mengambil postingan pengguna yang sedang login

Field post, posts, dan postCount hanya mengambil postingan dengan status "publish".

Untuk mengambil postingan dari pengguna yang sedang login, dengan status apa pun ("publish", "pending", "draft", atau "trash"), gunakan field berikut:

  • myPost
  • myPosts
  • myPostCount
query {
  myPosts(filter: { status: [draft, pending] }) {
    id
    title
    status
  }
}

Membuat postingan

Hanya pengguna yang sedang login yang dapat membuat postingan.

mutation {
  createPost(
    input: {
      title: "Hi there!"
      contentAs: { html: "How do you like it?" }
      status: draft
      tags: ["demo", "plugin"]
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
      ...on GenericErrorPayload {
        code
      }
    }
    postID
    post {
      status
      title
      content
      url
      date
      author {
        id
        name
      }
      tags {
        id
        name
      }
    }
  }
}

Memperbarui postingan

Hanya pengguna dengan kemampuan yang sesuai yang dapat mengedit postingan.

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

Query ini menggunakan nested mutation untuk memperbarui postingan:

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