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

Use wp_query when the query is set to inherited #41348

Closed
wants to merge 1 commit into from

Conversation

huubl
Copy link
Contributor

@huubl huubl commented May 25, 2022

What?

Use the actual wp_query when 'inherit query' in the query block is set.

Ps. I think this can't be merged as these lines are there probably for a reason. So maybe it should be fixed an other way.

Why?

When setting 'inherit query' in the query block it's not using the actual wp_query. This gives problems in some cases. For example current_post is not inherited and showing as -1.

Maybe fixing this too:
#39285

How?

Testing Instructions

  1. Use Twenty Twenty-Two + Gutenberg plugin
  2. Make a page and set it as 'Posts page' in settings > reading. Add the Query Loop Block with 'inherit query' set to the page.
  3. Add this to functions.php:
add_filter('render_block', function(string $block_content, array $block, WP_Block $instance) {
	// PS. is using empty($instance->context) the right way to target the actual post items?
	if ('core/post-template' === $block['blockName'] && empty($instance->context)) {
		global $wp_query;
		$block_content .= 'current_post: '.$wp_query->current_post;
	}

	return $block_content;
}, 20, 3);
  1. Watch output.

Screenshots or screencast

--

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label May 25, 2022
@github-actions
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @huubl! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@huubl huubl marked this pull request as ready for review May 25, 2022 18:14
@huubl huubl requested a review from ajitbohra as a code owner May 25, 2022 18:14
@aristath aristath requested review from ntsekouras and aristath May 26, 2022 11:34
@skorasaurus skorasaurus added the [Block] Post Template Affects the Post Template Block label May 26, 2022
@huubl huubl linked an issue Jun 6, 2022 that may be closed by this pull request
@ntsekouras
Copy link
Contributor

Thanks for the pr @huubl! There are a couple of issues with the inherit option and I'm trying to better understand the problems and a good path forwards. This is quite related to this PR about sticky options.

At first I'd like to understand what problems you're facing with the current approach. You mention:

This gives problems in some cases. For example current_post is not inherited and showing as -1.

current_post is available only during The Loop and the current changes doesn't seem to affect anything. That value is updated later in the loop. Does your PR fixes your use case? If yes, can you elaborate a bit on the use case?

What other challenges are you facing?

I think the big issue we're having with Query Loop and inherit is that while some controls are hidden in the UI, their values are preserved and I observed that while testing with at least orderby - this value is not set in the global query. We need to have no arbitrary results from block query attributes and what we expect when we set inherit to true.

Maybe we should inherit all options from global query but maybe there are some other concerns.. @draganescu had mentioned some nuances in the sticky use cases that I'm also planning to look into more..

@huubl
Copy link
Contributor Author

huubl commented Jul 4, 2022

Hi @ntsekouras. Thank you for looking into this.

Does your PR fixes your use case? If yes, can you elaborate a bit on the use case?

Yes, this pull request solves my use case. I need to build an irregular layout for posts. I build a custom (ACF) post block and use $wp_query->current_post to change the markup and image size depending on index of the post.

What other challenges are you facing?

I thought I had issues when using the pre_get_posts action, unfortunately can't reproduce them. But I can imagine there can be more related issues, like the PR you mention and maybe this issue.

We need to have no arbitrary results from block query attributes and what we expect when we set inherit to true.

I agree. A thought: Isn't it possible to alter wp_query early on with the desired block query attributes added somehow (like pre_get_posts)? This way the wp_query is used with the possibility to change e.g. orderby according to the block query attribute. Plus it saves an extra query.

@huubl
Copy link
Contributor Author

huubl commented Jul 26, 2022

Fixed by:
#40656

@huubl huubl closed this Jul 26, 2022
@desrosj
Copy link
Contributor

desrosj commented Oct 21, 2022

@huubl While collecting props and credits for 6.1, it seems you are one of a few dozen folks that have not connected their WordPress.org account to their GitHub profile.

Could you share your WordPress.org account so that we can ensure you're properly credited on the About page for WordPress 6.1? Connecting your accounts will also help those collecting this in the future. Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Post Template Affects the Post Template Block First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Query Block: current_post is showing as -1 when using 'inherit query'
4 participants