Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: acf fields not resolving with preview #156

Merged
merged 6 commits into from
Jan 19, 2024

Conversation

jasonbahl
Copy link
Contributor

What does this implement/fix? Explain your changes.

This migrates some code from the previous version of WPGraphQL for ACF that got missed during the re-architecture.

Does this close any currently open issues?

closes #154

Any other comments?

Because of the missing code that wasn't ported over during the re-architecture of the plugin, querying fields on posts while using asPreview: true were always returning null.

Now, querying a preview will return the un-published revision data:

Published Post

The published post shows the latest values that were published.

CleanShot 2024-01-18 at 16 31 56

Preview of the Post

If you click "preview" when editing a post (not Update or Publish), then query for it in WPGraphQL, you will see the un-published values, stored on the "revision" post.

CleanShot 2024-01-18 at 16 30 37


NOTE: The WordPress Block Editor (aka Gutenberg) has a bug that prevents Meta from being saved while using the block editor. This means that using ACF Field Groups as meta boxes on posts that also use the Block Editor beans the underlying data will not be stored on the preview and therefore cannot be resolved properly.

Posts using the Block Editor will resolve the values from the parent (published) post instead when queried as a preview.

We have added a graphql_debug message to indicate this active Gutenberg issue:

CleanShot 2024-01-18 at 16 36 25

chore: prep for launch on the .org repo
…gistered to the Schema

- Use the registered field names and keys to determine wether or not to resolve the field from the post itself (if revision meta is supported) or the parent (if it's not).
@jasonbahl jasonbahl self-assigned this Jan 18, 2024
@jasonbahl jasonbahl requested a review from josephfusco January 18, 2024 23:37
@andremendonca03
Copy link

@jasonbahl Thanks for looking into it.

Unfortunately I still can't preview acf fields. This time, however, the query doesn't retrieve published data anymore, just null.

  1. When trying to query for a pages (post or cpt) preview, the ACF fields return null:

Screenshot 2024-01-19 at 1 09 47 pm

  • Only the page's title returns the preview data as expected.
  • One strange thing I notice is that when I click to preview the page, I'm still seeing the title "About Title" in WordPress default twenty-twenty-four theme (which in preview should display About Title Preview1 just as GraphQL does).

Screenshot 2024-01-19 at 1 13 55 pm

Just as a disclaimer: I'm not using any other plugin but WPGQL, ACF PRO and WPGQL-ACF with the changes from this PR. Default theme is twenty-twenty-four with a filter to remove guttenberg block editor.

@jasonbahl
Copy link
Contributor Author

When you click preview are you right clicking to open in a new tab? If so that doesn't generate the preview data

@andremendonca03
Copy link

Wow... I really didn't know that. It worked!!

Honestly I don't know why I did't left-clicked it before ;)

Thanks immensely for that.

@jasonbahl
Copy link
Contributor Author

Ya it's weird. But if you right click and open in a new tab it doesn't generate the nonce and so preview data isn't stored properly.

I wonder if I can detect this somehow and output a debug message of some sort. 🤔

@andremendonca03
Copy link

Yeah that would definitely be helpful for others!

josephfusco
josephfusco previously approved these changes Jan 19, 2024
Copy link
Member

@josephfusco josephfusco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to pull this down to test.

I observed this working properly when the classic editor is active. When using the default block editor (Gutenberg), previews are not working as already called out in this PR. The additional debug notice was very helpful 🙌🏻

@jasonbahl jasonbahl merged commit e2b2ac3 into develop Jan 19, 2024
@jasonbahl jasonbahl mentioned this pull request Jan 23, 2024
@jasonbahl jasonbahl deleted the fix/acf-fields-not-resolving-with-preview branch January 24, 2024 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ACF fields cannot be queried in previews
3 participants