Gatsby with wordpress does not find an ACF flexible content field if it is unused Gatsby with wordpress does not find an ACF flexible content field if it is unused wordpress wordpress

Gatsby with wordpress does not find an ACF flexible content field if it is unused


I think the issue you’re having is similar to a fairly common one I’ve also encountered when using Gatsby + the WordPress REST API.

A brief summary is that the WordPress REST API will, for example, return a boolean when there is an ACF gallery field with no images, rather than null, which is what the GraphQL query is expecting when the field is empty. I suspect the same thing is happening for you: you are querying for the subfields that haven’t been filled out, and you are getting a response that GraphQL interprets as the wrong type, instead of null. (Full disclosure, my only experience with GraphQL is via Gatsby.)

Luckily, I think there are a lot of options for you to resolve this.

New Gatsby solution

The Gatsby team and contributors have been working on this one pretty actively recently, and you can currently try out a preview version of the new approach here: https://www.gatsbyjs.org/blog/2019-03-04-new-schema-customization/

You can read a lot more about the issue and background here, if you want: https://github.com/gatsbyjs/gatsby/issues/3344

Existing, quick WordPress solutions

The two solutions that work around this now, if you don’t want to use something not completely merged into Gatsby yet:

  1. Setting empty responses to null for that ACF field as described by @pieh on the Gatsby team, and there are more examples in that GitHub issue for other field types
  2. Creating “dummy” content, where everything is filled out, and then that post is filtered out before being displayed (ex. when the slug is placeholder). The catch here is you have these fake posts for each Custom Post Type that cannot be deleted from WordPress.

I’ve used both approaches, and both work. I would say #2 was probably more reliable as it works for all the fields you are using at once, but is potentially more confusing depending on who is using the CMS: “Why are these posts here?”

Hope that’s helpful!