-
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
Query and Search blocks: support for Instant Search #63147
base: trunk
Are you sure you want to change the base?
Query and Search blocks: support for Instant Search #63147
Conversation
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @r-chrzan! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @rchrzan@whitelabelcoders.com, @ktmn. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Great work!! 👏👏 Let's see what's next 🙂
|
…of post-template/index.php
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 was testing the use cases outlined in my previous comment #63147 (comment).
I see that there are two issues still present. The proposed URL structure is incompatible with how WordPress works currently on the search results page. In particular, it is impossible to use two different URL params to control the same feature: s
and instant-search
. I tested it by modyfing the Search Results template and moving the Search block inside Query block.
Example URL:
https://playground.wordpress.net/scope:ambitious-noisy-town/?s=hello&instant-search=none
There is also a change in the behavior of the Search block with the button when the instant search is enabled. Without the feature, the user needs to click the button to trigger the search. With the feature, it gets triggered automatically on every keystroke. It's something to discuss further as it's a substantial change in the user experience.
@@ -187,6 +187,18 @@ function gutenberg_initialize_experiments_settings() { | |||
) | |||
); | |||
|
|||
add_settings_field( | |||
'gutenberg-search-query-block', | |||
__( 'Instant Search and Query Block', 'gutenberg' ), |
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.
What's the reasoning behind adding an experiment for this feature? In practice, it's just a new behavior on the Search block when placed inside the Query block.
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 reasoned that the feature should remain an experiment while we polish it.
I don't want the current PR to stay up for weeks or months. Instead, I'd prefer to merge an experimental version of the block, even if it's incomplete, and add the remaining functionality in follow-up PRs.
Thanks for the review @gziolo! You're right on both counts:
The PR is ready for another round of review, note the updated PR description. Here are some of the open questions that I'd like to discuss. Search query URL param syntax
Search submission behavior
Those two questions are related in my mind. If we can transparently inform the user in the editor that the Search block becomes an Instant Search block, then this Instant Search block should also have the correct behavior on the frontend that is different from the default Search block. The specific mechanism for doing this is not yet clear. Maybe we should be more explicit and add a toggle in the block settings that makes the Search block an Instant Search block? Regardless of the solution in the editor, the Instant Search block should (on the frontend):
Technical questions
|
// Store the original global $wp_query | ||
$temp_query = $wp_query; | ||
|
||
// Temporarily replace global $wp_query with a new query that includes the | ||
// search query because get_next_posts_link() uses global $wp_query. | ||
$wp_query = new WP_Query( $args ); | ||
$content = get_next_posts_link( $label ); | ||
|
||
// Restore the original global $wp_query | ||
$wp_query = $temp_query; |
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.
Are there any issues with replacing the global $wp_query
and restoring it after calling get_next_posts_link()
?
That’s fair. I’m fine with adding enhancement in steps. What’s the reasoning for adding the global handling for the default query in this PR if it isn’t expected to be functioning in the current shape?
It’s definitely something that needs to be further investigated. What’s the reason the same query needs to be recomputed for every child block? Can it be shared? What prevents from using a WP filter to modify the vars passed to the existing query in Gutenberg?
Is there a way to pass a canonical URL without the pagination applied to use it instead without all the complexity explained? |
Is the information for how a URL should be constructed for a given site available somehow? |
That's a good point. I'm happy to handle the default queries in another PR if that makes is easier to review. I will split the current PR up.
The reason is that the In the follow-up PR (handling the global/inherited query) I can try to use the
I'm not aware of one. I took a look, and there is a In general, I don't think it is possible to do it reliably server-side in PHP. A site could also, e.g., sit behind an Nginx proxy, which could be serving on a different URL and a WordPress function would have no way of knowing it. |
@sirreal I think that the closest resource would be https://developer.wordpress.org/themes/basics/template-hierarchy/ The final URL depends on both the permalink settings and the Template hierarchy. Is that what you had in mind? |
Sorry that was unclear, I was wondering if the site can be queried to determine whether a search param should be used or some other form. For this initial version the redirect seems alright and it can be improved in the future. It may be able to observe the redirect and adjust behavior accordingly. |
What?
Initial implementation for Instant Search using the Search and Query Loop blocks. Added as a new experiment on the Gutenberg Experiments page.
Related to #63053
How does it work?
Force Page Reload
i.e. use "enhanced pagination" in that Query block.When the Search block is inside of the Query block using "enhanced pagination", it automatically gets "updated" to an Instant Search block.
Any search input in the Instant Search block gets passed as the
?instant-search=<your-search>
query search param in the URL.Multiple Query & Search blocks
It's possible to have multiple Instant Search blocks in a page/post/template. In such a case, ensuring their functionality is isolated is essential. It's also important to remember that the "query type" in the Query block can be
Default
orCustom
. TheDefault
query is inherited from its respective template. For this reason, the Instant search block uses different URL search params when handling each:Default
-?instant-search=<search-term>
Custom
-?instant-search-<queryId>=<search-term>
Limitations
Default
query on in the same template are currently not supported.Pagination
The instant search functionality intersects with pagination of the Query block:
Further work
This is an initial prototype and intentionally does not yet implement all the features that should be expected in the final version. Those include but are not limited to:
Ensure that it works correctly for inherited queries.Pagination - Reset page number for every search.The search parameter in non-inherited queries should work correctly (as mentioned in https://github.com/WordPress/gutenberg/pull/63147#issuecomment-2214460127)`Should work when there is more than one Query Loop block & Search block on the page.Ensure all user input is sanitized / validated.Default
queries.a11ySpeak()
of theinteractivity-router
here and here.Video
output_62afd8.mp4