-
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
Provide API to create Query Block Variations #25607
Comments
Hey @mariohamann - thanks for your time on this and all the feedback in the design issue! For reference the tracking issue for As I usually say in discussions for Both tracking and design issues mention the To me it seems that the ultimate goal here to have the query related attributes to Also we shouldn't need a What the For example to achieve the first use case (
Regarding use cases 2 and 4, a related comment by me is here:#25503 (comment). In a gist, while they are good explorations, I believe are out of Query block's scope. These could be new blocks since they are generic. For example a Carousel block could show just images. A lot of the I would propose to close this issue and look for a way forward with the existing APIs. Next steps would be to make progress in the design you've also been part of here: #25198 and see what display options would be useful for |
Thank you very much for your feedback, your clarifications and your thoughts! I realize that I'm thinking too much from the PHP perspective (more thinking of WP_Query instead of Summarizing the Query Block concept for myself
Revising the "API concept"So thinking of a Custom Post Type "jobs":
I'm completely fine with closing the issue or giving it less focus for the moment especially facing 5.6! But as e. g. @sc0ttkclark asked for a way to easily extend the Query Block in #24934 (comment), maybe it's a good starting point for further development after 5.6? |
Correct.
Not quite, but the result is the same. Blocks inside Query get query params from React Custom taxonomies and custom post types support should be implemented and in general with the progress of Query block, there will be needed additions to the REST API.
What do you mean here? Like what?
The query block could be extended like any other block with filters API: https://developer.wordpress.org/block-editor/developers/filters/block-filters/. If you feel I didn't answer something or needs clarification, please ask. :) |
Thanks for all clarifications!
As described in #25377 (comment) in my opinion this should be possible for even a MVP. Maybe you can have a look over there?
A variation for woocommerce products would use
Again thinking more from the PHP world: We have the filter apply_filters( 'render_block', $block_content, 'name_of_block_variation ){
$posts = json_decode($block_content);
?>
<ul>
<?php
foreach($posts as $post){
?>
<li>
<a href="<?= $post->link; ?>">
<?= $post->title->rendered ?>
</a>
</li>
<?php
}
?>
</ul>
<?php
}
(Working example for the inner part of the filter incl. simplified JSON-Output of Summary:
|
Yes. It definitely needs a lot of work. Just consider that this block was introduced in code base just about three months ago. I have actually started exploring the implementation of custom post types.
Actually the
That would be great and we will work towards that :) I don't have all the answers yet, but iterating on this will help all of us find the best path forward. |
Hi, @mariohamann and @ntsekouras. This discussion touches a lot of different parts, so I'll jump right to a few observations:
The Query Loop block, responsible for the actual querying (whereas Query is solely responsible for managing the intended query params), relies on two systems for querying:
I'd like to address the phrase "Now we need a new block". Needing a new block shouldn't be a problem. :) There's always a search for balance in Gutenberg between providing blocks that are highly generic and flexible but hard to master, and providing a larger number of specialised blocks that are more likely to serve most user needs with barely any learning curve. The balance sits somewhere in between both ends of the spectrum. This is why Gutenberg provides both a Columns block and a Media & Text block. The point I'm making is that creating dedicated blocks is encouraged. It's also why there's a lot of emphasis on providing good reusable components and solid APIs for blocks and querying. In my mind, something like #25505 is a much better candidate for a dedicated block type — whether it's a core type or a third-party type.
Enqueueing of scripts and styles is already supported natively in the framework on a per-block-type basis, reinforcing the idea that, oftentimes, a dedicated block really is the best choice. |
Thanks for your efforts and exploration here Mario! I'll close this issue now as we have moved forward with using the current |
@mapk mentioned the idea of block variations for Query Block in #25198 and in a chat we had yesterday (thanks again! :)) – and I would like to expand this idea.
Base: The Query Block
This is the base for all following variations and is used in FSE. In FSE it should get the most important paramaters from the theme (no need to select e. g. Categories in archive.php – it would completely break the current logic of WP archives.) Therefore it just needs some basic settings in the sidebar. Those (default) settings could be defined as:
These parameters would provide the following block settings in sidebar:
Use Case 1: List some posts
There are a lot of use cases to show some defined blocks in a page, e. g. "Hall of fame" (3 most commented posts), "Topic specific posts" (showing e. g. 5 of posts of a specified category) etc. The idea is: Having a strong API we could simply create a new variation of the Query Block, passing some paramters and we are done.
functions.php
query_params.json
This would create the following for sidebar settings, making it possible to create e. g. a Hall of fame of similiar:
Use Case 2: List some posts – with different callback
@iamtakashi provided some nice ideas for the query block: #25502 #25501 #25503 In fact, if a Plugin Developer could inherit the settings of the Block above and if s:he could change the callback to have a different output in an API, it would be no problem to add some JavaScript, different HTML etc. etc. The Query block just returns the post – the output is handled by the callback.
functions.php
Use Case 3: Custom Post Type
I often create Custom Post Types, e. g. for Jobs, Events, Persons. For my own CPTs I often find myself writing a simple Shortcode to create a simple Query, listing e. g. Jobs. If I want a better UI for authors I invest some more time and create a block (I have to admit: often with ACF...) to let them select e. g. categories. Short codes feel and are old fashioned, Block development is often very time consuming.
If we could write Block variations with a strong API, I would just need to register the variation with its own callback and add a new
query_params.json
:Use Case 4: List Categories
We already have a block (or widget?) to list categories. If we could use the Query Block as a base, the whole code to register the block with its settings would be:
Use Case 5: Single Post Selector
There exist a lot of plugins, which are based on the idea of showing a single post of custom post type, e. g. showing a single Contact Form, being a CPT (Caldera Forms, CF7). If the API would be strong enough, a plugin author could simply write the following code to return one single Post:
Further ideas
Showing selectors for meta fields, Media queries to list images, multiple select defined single posts (like in WooCommerce Blocks to list specified blocks), show products of a category etc. etc. – the possibilities are endless. :)
I hope it's fine to cc some other people @ntsekouras @aristath @annezazu
Here is a Figma file with every Use case I described above with description, PHP and JSON examples, sidebar and possible output: https://www.figma.com/file/YE8pjhnO0vHzFx7iDUW9mW/Query-Block-variations?node-id=0%3A1
The text was updated successfully, but these errors were encountered: