WordPress 데이터 쿼리
WordPress 데이터 쿼리포스트

포스트

포스트 데이터를 가져오고 수정하는 쿼리 예시입니다.

포스트 가져오기

작성자와 태그를 포함한 단일 포스트:

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

댓글을 포함한 포스트 5개 목록:

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

지정된 포스트 목록:

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

포스트 필터링:

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

포스트 결과 수 세기:

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

포스트 페이지네이션:

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

태그가 있는 포스트:

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

카테고리가 있는 포스트:

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

메타 값 가져오기:

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

로그인된 사용자의 포스트 가져오기

post, posts, postCount 필드는 상태가 "publish"인 포스트만 가져옵니다.

로그인된 사용자의 포스트를 임의의 상태("publish", "pending", "draft" 또는 "trash")로 가져오려면 다음 필드를 사용하세요:

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

포스트 생성

로그인된 사용자만 포스트를 생성할 수 있습니다.

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

포스트 업데이트

해당 권한을 가진 사용자만 포스트를 편집할 수 있습니다.

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

이 쿼리는 중첩된 mutation을 사용하여 포스트를 업데이트합니다:

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