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

Blocks unavailable when new WP_Query is used inside render callback #8035

Closed
jomurgel opened this issue Jul 18, 2018 · 2 comments · Fixed by #9182
Closed

Blocks unavailable when new WP_Query is used inside render callback #8035

jomurgel opened this issue Jul 18, 2018 · 2 comments · Fixed by #9182
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions. [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes.

Comments

@jomurgel
Copy link
Contributor

Describe the bug
Use of a new WP_Query throws no errors, but breaks admin render of blocks using them.

To Reproduce
Steps to reproduce the behavior:

  1. Utilize new WP_Query over a get_posts or get_recent_posts function
  2. Update block and save.
  3. Refresh page (or exit and return)
  4. Will see a random post or will return a post in class editor.

post_content does, in fact, save the block data as expected and front-end render is fine. This effects only admin-facing blocks.

Potentially Related
#5572
#7427
#4733

Expected behavior
Reload/return to post, should see Gutenberg block.

Screenshots
Block added:
screenshot 2018-07-18 13 18 19

Refresh (when new WP_Query is used instead of wp_get_recent_posts ):
screenshot 2018-07-18 13 16 59

Desktop (please complete the following information):

  • OS: MacOS 10.13.6
  • Browser ALL

Additional context

  • Occurs with v 3.2
@chrisvanpatten
Copy link
Contributor

chrisvanpatten commented Jul 18, 2018

I downloaded your block and messed around a bit. To be more specific, it's not exactly related to WP_Query, it's the use of $query->the_post().

If you run the WP_Query and just use the $query->posts array directly (iterate over it, directly access $post->title, $post->ID, etc.) it will work okay, because it won't override the global $post.

Your change to use get_post() avoids a the_post() call which is why it works while the WP_Query version didn't.

Also related to #7582 and core #18408.

You can also cheat a bit and save the global $post before resetting it, then set it again after your query. There's a good example here courtesy of @aduth.

@designsimply designsimply added [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes. [Type] Help Request Help with setup, implementation, or "How do I?" questions. labels Jul 18, 2018
@obenland
Copy link
Member

I downloaded your block

Where can this block be found?

obenland added a commit that referenced this issue Aug 20, 2018
Stores and restores the global post object around dynamic block callbacks. This allows dynamic blocks to create new `WP_Query` instances and set up its post data, without changing the currently edited post.

See #7427, #8035.
obenland added a commit that referenced this issue Aug 21, 2018
Stores and restores the global post object around dynamic block callbacks. This allows dynamic blocks to create new `WP_Query` instances and set up its post data, without changing the currently edited post.

Fixes #7427, fixes #8035.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions. [Type] Plugin Interoperability Incompatibilities between a specific plugin and the block editor. Close with workaround notes.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants