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

wp_reset_query() in a block doesn't work #7427

Closed
manake opened this issue Jun 21, 2018 · 2 comments · Fixed by #9182
Closed

wp_reset_query() in a block doesn't work #7427

manake opened this issue Jun 21, 2018 · 2 comments · Fixed by #9182
Labels
[Feature] Block API API that allows to express the block paradigm. [Type] Question Questions about the design or development of the editor.

Comments

@manake
Copy link

manake commented Jun 21, 2018

function xxx() {
	$something = apply_filters( 'the_content', '<!-- wp:slug/something /-->' );
}
add_action( 'admin_enqueue_scripts', 'xxx' );

function render_callback_for_slug_something(){
	$recent = new WP_Query( $args );

	while ( $recent->have_posts() ) {
		$recent->the_post();
	}

	wp_reset_query();
}

With the above code present when I try to edit a page like:
http://localhost/wp-admin/post.php?post=111&action=edit

Then that URL redirects me to some other ID (of some post):
http://localhost/wp-admin/post.php?post=222&action=edit

In other words wp_reset_query(); didn't restore $post? How can I make a custom query in a PHP render callback?

Gutenberg 3.0.1

@mtias mtias added [Type] Question Questions about the design or development of the editor. [Feature] Block API API that allows to express the block paradigm. labels Jul 19, 2018
@obenland
Copy link
Member

Is your code snippet a fully working example? I'm having a hard time recreating this

@manake
Copy link
Author

manake commented Aug 20, 2018

Yes, I reduced this to all necessary parts.

And yes, this definitely was happening and I remember debugging it down to the usage of $recent = new WP_Query( $args ); and to not working wp_reset_query();. So, these two lines were causing this.

I don't have time to test this again now, if you believe this is not happening you can close this and I can reopen this later if it'll be happening.

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
[Feature] Block API API that allows to express the block paradigm. [Type] Question Questions about the design or development of the editor.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants