Home > Enterprise >  gatsby-source-wordpress: WP categories return undefined
gatsby-source-wordpress: WP categories return undefined

Time:02-28

There seems to be an error in the Gatsby WPGraphQL Plugin. In GraphQL under localhost:8000/___graphql I get my categories name and id returned for a post with the following query:

query MyQuery {
  wpPost(title: { regex: "/Works/" }) {
    categories {
      nodes {
        id
      }
    }
  }
}

GraphQL query

However, in Gatsby the same query does not return anything, when I say

<p>{data.wpPost.categories.nodes.id}</p>

there is no error and the paragraph element get's rendered but without any content. I have also tried it with name, slug and basically any other of the available elements in "category" with the same result: an empty element.

Any help would be greatly appreciated!

CodePudding user response:

nodes is an array of data (specifically an array of objects, as you can see in the result query: "nodes":[]) so to point directly the data you must enter an specific position or loop through the nodes.

This should work:

<p>{data.wpPost.categories.nodes[0].id}</p>
  • Related