-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add a filter to build_query_vars_from_query_block
#43590
Add a filter to build_query_vars_from_query_block
#43590
Conversation
The filter allows Query Loop block variation to hook into the query and possibly inject custom query parameters.
@sunyatasattva, thank you for opening this PR. I see this filter applicable in many more cases. It would help to handle custom post types or even custom fields for existing REST API endpoints registered by plugins 👍🏻 The way to move forward with this PR would be to include the documentation for the hook like here in this repo: gutenberg/packages/block-library/src/navigation/index.php Lines 334 to 345 in 79dcf05
or like here in WordPress core: We also need to settle on the name of the hook that is going to stay forever. It can't be prefixed with We will also need to move the updated function from |
Thanks for the PR @sunyatasattva! Everything @gziolo said is great 😄 We also need to look into the editor side(related: #34201), but it can happen in a different PR. |
Php docs are a must have. |
Hello @gziolo thank you for your review. I pushed another commit, let's see if I've understood correctly:
The reasoning for my proposal on that name is that:
Let me know what you think and if I did everything right. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for iterating @sunyatasattva! 💯
if ( ! empty( $block->context['query']['search'] ) ) { | ||
$query['s'] = $block->context['query']['search']; | ||
} | ||
if ( ! empty( $block->context['query']['parents'] ) && is_post_type_hierarchical( $query['post_type'] ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly enough this code from here was close to the 6.0 release and didn't land there, so we should have moved this file to compat/6.1
either way.. Good timing for this change 😄
* Change filter name to `query_loop_block_query_vars` * Fix docs typo * Fix docs version Co-authored-by: Nik Tsekouras <ntsekouras@outlook.com>
@ntsekouras Feedback implemented, let me know if there is anything else that should be done 🙏 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, thanks @sunyatasattva! This looks good to me! 💯
I'd appreciate a final look from @gziolo, but I think it's good to 🚢
* This can help, for example, to include additional settings or meta queries not | ||
* directly supported by the core Query Loop Block, and extend its capabilities. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the key part of the comment. Anything already supported by the block should be done by the block. For example we have special handling for offset
based the perPage and page
attribute, and if someone would alter only the perPage
from the filter, would end up with wrong offset in the pagination..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, I see that. I guess that's the risk with many filters, right? Each developer needs to be responsible and see what kind of side effects they might be introducing…
Awesome, thank you @sunyatasattva for bringing it to the finish line. Once, we have also the matching API in the editor, let's make sure it is documented in the Block Editor Handbook somewhere in the hooks section: https://github.com/WordPress/gutenberg/tree/trunk/docs/reference-guides/filters |
@sunyatasattva, can you resolve conflicts in this branch. It looks like I landed changes added in exactly the same place and git has some trouble understanding those are different features and both should be there 😅 |
Detail and explain how to make best use of the changes introduced in the following PRs: WordPress#43590, WordPress#43632 and WordPress#44093
…enberg repository. This changeset backports changes from the following Gutenberg pull requests: - [WordPress/gutenberg#43590 gutenberg#43590] Add a filter to `build_query_vars_from_query_block` - [WordPress/gutenberg#40933 gutenberg#40933] Block Library - Query Loop: Add parents filter Props ntsekouras, bernhard-reiter. See #56467. git-svn-id: https://develop.svn.wordpress.org/trunk@54175 602fd350-edb4-49c9-b593-d223f7449a82
…enberg repository. This changeset backports changes from the following Gutenberg pull requests: - [WordPress/gutenberg#43590 gutenberg#43590] Add a filter to `build_query_vars_from_query_block` - [WordPress/gutenberg#40933 gutenberg#40933] Block Library - Query Loop: Add parents filter Props ntsekouras, bernhard-reiter. See #56467. Built from https://develop.svn.wordpress.org/trunk@54175 git-svn-id: http://core.svn.wordpress.org/trunk@53734 1a063a9b-81f0-0310-95a4-ce76da25c4cd
…enberg repository. This changeset backports changes from the following Gutenberg pull requests: - [WordPress/gutenberg#43590 gutenberg#43590] Add a filter to `build_query_vars_from_query_block` - [WordPress/gutenberg#40933 gutenberg#40933] Block Library - Query Loop: Add parents filter Props ntsekouras, bernhard-reiter. See #56467. Built from https://develop.svn.wordpress.org/trunk@54175 git-svn-id: https://core.svn.wordpress.org/trunk@53734 1a063a9b-81f0-0310-95a4-ce76da25c4cd
As we worked with Gutenberg folks in WordPress/gutenberg#43590, WordPress/gutenberg#/43632 and WordPress/gutenberg#/44093 we have created a standard API that could be used for our use-case. This PR refactors our WIP experimental work to use that standardized API.
…enberg repository. This changeset backports changes from the following Gutenberg pull requests: - [WordPress/gutenberg#43590 gutenberg#43590] Add a filter to `build_query_vars_from_query_block` - [WordPress/gutenberg#40933 gutenberg#40933] Block Library - Query Loop: Add parents filter Props ntsekouras, bernhard-reiter. See #56467. Built from https://develop.svn.wordpress.org/trunk@54175
* Refactor Product Query to use the latest Gutenberg APIs As we worked with Gutenberg folks in WordPress/gutenberg#43590, WordPress/gutenberg#43632 and WordPress/gutenberg#44093 we have created a standard API that could be used for our use-case. This PR refactors our WIP experimental work to use that standardized API.
* Add docs for extending the Query Loop block via variations Detail and explain how to make best use of the changes introduced in the following PRs: #43590, #43632 and #44093 * Fix typo Change enhables to enables Co-authored-by: Sören Wrede <soerenwrede@gmail.com> * Address code review feedback Specifically: * Added the complete code at the beginning * Change `isActive` from array to function * Reworded a few things * Added information about custom logic and other hints * Update docs/how-to-guides/block-tutorial/extending-the-query-loop-block.md * Change wording of opening paragraph for the example * Add section about innerBlocks and `scope` shortcomings * rename to `allowedControls` Co-authored-by: Sören Wrede <soerenwrede@gmail.com> Co-authored-by: Ryan Welcher <me@ryanwelcher.com> Co-authored-by: Nik Tsekouras <ntsekouras@outlook.com>
* Refactor Product Query to use the latest Gutenberg APIs As we worked with Gutenberg folks in WordPress/gutenberg#43590, WordPress/gutenberg#43632 and WordPress/gutenberg#44093 we have created a standard API that could be used for our use-case. This PR refactors our WIP experimental work to use that standardized API.
…enberg repository. This changeset backports changes from the following Gutenberg pull requests: - [WordPress/gutenberg#43590 gutenberg#43590] Add a filter to `build_query_vars_from_query_block` - [WordPress/gutenberg#40933 gutenberg#40933] Block Library - Query Loop: Add parents filter Props ntsekouras, bernhard-reiter. See #56467. git-svn-id: https://develop.svn.wordpress.org/trunk@54175 602fd350-edb4-49c9-b593-d223f7449a82
…7169) * Refactor Product Query to use the latest Gutenberg APIs As we worked with Gutenberg folks in WordPress/gutenberg#43590, WordPress/gutenberg#43632 and WordPress/gutenberg#44093 we have created a standard API that could be used for our use-case. This PR refactors our WIP experimental work to use that standardized API.
What?
This PR adds a filter to the
build_query_vars_from_query_block
which is used to parse aWP_Query
and pass it down the Query Loop block context.Why?
This allows Query Loop block variations to hook into the query and possibly inject custom query parameters, enabling developers to support all kinds of custom queries and create powerful variations on the Query Loop block.
For an example on how this can be used to achieve this goal, see this PR: woocommerce/woocommerce-blocks#6812 (specifically, this filter is used here).
Paging @gziolo and @ntsekouras.
How?
The minimal change to enable this is to wrap the return value of the function in a filter.
Testing Instructions
A way to test the practical use of this change would be to use the above-mentioned PR to see how it could work. Or just pull from
trunk
on WooCommerce Blocks.