-
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
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. |
We could get the value of But this would not help us much. We'd still have to update the URL on the client side. And for this, we need to parse the URL on the client side to e.g update |
There is still a lot of code repetition for every core block that can be nested inside the Query block. In the case of this PR, nearly the same changes have to be applied to 5 existing core blocks. Unfortunately, the same logic won't be available to custom blocks. I explored some alternatives, and I was surprised at how powerful it could be to use a filter, for example: function block_core_query_add_url_filtering( $context ) {
if ( empty( $context['queryId'] ) ) {
return $context;
}
$search_key = 'query-' . $context['queryId'] . '-s';
if ( ! isset( $_GET[ $search_key ] ) ) {
return $context;
}
$context['query']['search'] = sanitize_text_field( $_GET[ $search_key ] );
return $context;
}
add_filter( 'render_block_context', 'block_core_query_add_url_filtering' ); It's a quick proof of concept. This would execute too many times, as it would be applicable to every block nested inside the Query, but it's quite powerful. It probably could be improved by checking the parent block. It basically enabled filtering by a search term provided in the URL targeting a specific custom query. Example URL: In my testing, it turned out the URL structure for custom queries doesn't follow the settings for permalinks. The example I shared uses numeric permalinks for the default query, but not for custom queries. The open question is what would be the ultimate fix in WordPress core. The challenge is that the context is exposed based on the |
That's interesting, thanks for exploring it @gziolo. I think that this approach might work. I'm going to try it now - we can definitely make it only run when the an ancestor block is Query Loop and has enhanced pagination enabled, etc.
Yup, that's true. Only the default queries use the permalink settings, the custom queries use the
So this means that the latest query (with search parameter) always gets saved in the database? And the problem is that a request might NOT include any (instant) search param, but the child blocks (query-pagination, query-numbers, etc.) could not distinguish between Regardless, I'll give this approach a try 🙂 |
I need some clarification on how this is a problem. 🤔 We're only overriding the context at runtime when a request is being made. So the |
I've started to explore the alternative in #67013. We can discuss there 🙂 . |
This case is now handled via baa6188. The remaining doubt I have is how to handle the URL. Should we (on the fronted) modify the URL to include a query param like output_9d447c.mp4 |
Right, that's correct. I think we're talking about the same thing 🙂. I just did not realize that one can update the search term in the UI in addition to simply editing the markup (I did the latter in this example). The effect is the same either way: the It should now work as of baa6188. Whenever the user updates the search term (via UI or just directly editing the markup), the search term will appear in the Search input on the frontend upon loading the page. What I was considering here:
is something different. I wondered: What to do when loading a page for the first time, including an Instant Search block with a custom search term. In the video in the previous comment I show that the word However, I gave it some more thought, and it's probably not a good idea and would generate more problems than solve, I think. We should not just update the links in JS after the page loads based on the page's contents. |
I will let other folks to help bring it to the finish line.
I discussed with @ntsekouras some options for the overall strategy of making the Query block filterable on the front end through URL params on WordPress Slack (https://make.wordpress.org/chat/): |
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