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

Pass outer context variable into query loop #31272

Open
carlomanf opened this issue Apr 28, 2021 · 1 comment
Open

Pass outer context variable into query loop #31272

carlomanf opened this issue Apr 28, 2021 · 1 comment
Labels
[Block] Query Loop Affects the Query Loop Block Needs Technical Feedback Needs testing from a developer perspective. [Type] Enhancement A suggestion for improvement.

Comments

@carlomanf
Copy link

What problem does this address?

I'm making a block that implements a form of post-to-post relationships (in a private repository at the moment) and it needs to be able to access the value of the postId context variable outside a query loop when the block is inside the query loop.

What is your proposed solution?

I was able to achieve this by having the query loop pass the hard-coded context to apply_filters with the outer context as a second argument, and then using this and other filters in my own plugin.

		$block_content = (
			new WP_Block(
				$block->parsed_block,
				apply_filters(
					'query_loop_context',
					array(
						'postType' => get_post_type(),
						'postId'   => get_the_ID(),
					),
					$block->context
				)
			)
		)->render( array( 'dynamic' => false ) );

Possibly, it could also be done by replacing WP_Block::render with render_block, because render_block already has a filter that appears to do the same thing.

Whichever implementation, there should be a pathway for blocks inside a query loop to be able to access the outer context.

@carlomanf
Copy link
Author

carlomanf commented Apr 28, 2021

To clarify, the fact that postId is passed with a different value is not really relevant. The point is that any outer context variable is inaccessible from within the query loop, whether it's postId or something else, whether it gets passed with a different value or whether it just doesn't get passed at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Query Loop Affects the Query Loop Block Needs Technical Feedback Needs testing from a developer perspective. [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

2 participants