WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.56k stars 4.22k forks source link

Missing Query block functionality #24934

Open ntsekouras opened 4 years ago

ntsekouras commented 4 years ago

With the ongoing work on FSE (https://github.com/WordPress/gutenberg/issues/24551) the Milestone:5 is about Query block. The tracking issue for Query block changes/enhancements is here: https://github.com/WordPress/gutenberg/issues/24762.

This issue will be an overview for identifying and adding more options/parameters in Query block.

The goal is to identify which parameters from WP_Query and the REST API can and will be supported from Query block.

There will be definitely some work in REST API to support some missing parameters and options if it makes sense and there are no blockers, like the orderby:rand param that has performance issues . An example for needed missing support is order by comments (comment_count): https://github.com/WordPress/gutenberg/issues/22002.

The below list is just the starting point for identifying the parameters to be supported and it will be finalised after discussions with everyone in the community about what parameters should eventually be supported. For now it contains items that are supported and/or make sense to me to be included. Feel free to share your thoughts and add/edit things to make this complete.

WP_Query reference: https://developer.wordpress.org/reference/classes/wp_query/ Posts API reference: https://developer.wordpress.org/rest-api/reference/posts/#list-posts

Supported from REST API

Argument:Option Started Completed
page
per_page
offset
categories
post_type
order
orderby:title
orderby:date
orderby:author 🔲 🔲
orderby:parent 🔲 🔲
orderby:modified 🔲 🔲
tags_exclude 🔲 🔲
categories_exclude 🔲 🔲
before 🔲 🔲
after 🔲 🔲
author
author_exclude 🔲 🔲
sticky
tax_relation 🔲 🔲
include 🔲 🔲
exclude 🔲 🔲

NOT Supported from REST API

Argument:Option Started Completed
orderby:comment_count 🔲 🔲
orderby:menu_order 🔲 🔲

Prioritize

Argument:Option Started Completed
tags
search

Display options

We should also provide some different display options for the Query block itself and possible children Post blocks like PostTitle, PostExcerpt etc. to enable users to create a list of entities which is highly customizable. This would also allow us to have more block variations and patterns.

Query block design

Issue about the design is here: https://github.com/WordPress/gutenberg/issues/25198

kjellr commented 4 years ago

I think we should definitely prioritize tags first. Then we'll have both category + tags working correctly. It's really common for themes to utilize a "featured" tag to posts to display them on the homepage. Once something like that is working for block-based themes, that'll be a big plus.

I think search is necessary to get a search results page template working, so that seems like a good second priority from my perspective.

ntsekouras commented 4 years ago

Added order and orderby (title + date) support here: https://github.com/WordPress/gutenberg/pull/24691

TimothyBJacobs commented 4 years ago

Ideally, this would be supporting taxonomies, not categories and tags specifically.

sc0ttkclark commented 4 years ago

Is there any possible way for a plugin to extend the query block for it's own purposes? For instance, a custom fields plugin that would like to add custom field filtering functionality or advanced filtering options. Does that solution have to extend the query block JS itself and then build out the Edit/Inspector Panel interface on the plugin dev's side?

Asking because I think we might see this happen quite a bit for this block.

ntsekouras commented 4 years ago

Hey @sc0ttkclark !

Is there any possible way for a plugin to extend the query block for it's own purposes? For instance, a custom fields plugin that would like to add custom field filtering functionality or advanced filtering options. Does that solution have to extend the query block JS itself and then build out the Edit/Inspector Panel interface on the plugin dev's side?

The query block could be extended like any other block with filters API: https://developer.wordpress.org/block-editor/developers/filters/block-filters/. Any extension though should be supported by the REST API.

Since there are definitely some things missing and more will be found that are needed, REST API should also be changed to support more things.

noisysocks commented 4 years ago

https://core.trac.wordpress.org/ticket/49983 contained a request for sorting by tags.

ianstewart commented 3 years ago

Not related to the querying itself but useful for pattern/theme creation: https://github.com/WordPress/gutenberg/issues/28532

bobbingwide commented 3 years ago

Some more requirements to add to the list.

Argument Use case
post_type Ability to select multiple post types
post_type Ability to select attachments
post_type Ability to select post types which aren't supported by the REST API
meta_key & meta_value Ability to use meta key and meta value to limit results
customcategoryname Ability to select custom taxonomy terms
post_parent Ability to select post parent ( see #31063 )
post_mime_type Ability to select post_mime_type. e.g. PDF attachments
post_status Ability to select post_status e.g. schedule
include / exclude Ability to select posts to include / exclude
IDs Ability to select specific post IDs - so that they can be formatted by query-posts
bradhogan commented 2 years ago

Any updates on where post_parent and menu_order are in the updates for the query block?

danburzo commented 2 years ago

Any updates on where post_parent and menu_order are in the updates for the query block?

These are exactly the two options that I currently miss the most. Note that switching to the code editor and adding menu_order manually as the sorting works (at least in WP 5.9.3). (Not so for post_parent.)

dashkevych commented 2 years ago

I think we should definitely prioritize tags first. Then we'll have both category + tags working correctly. It's really common for themes to utilize a "featured" tag to posts to display them on the homepage. Once something like that is working for block-based themes, that'll be a big plus.

I think search is necessary to get a search results page template working, so that seems like a good second priority from my perspective.

I agree that having a tag filter would be very useful for highlighting special posts on a page (functionality that is similar that Jetpack has for Featured Content).

However, it would be also very useful to have a functionality in Query block to exclude posts by tag to avoid duplicates.

For instance, the page has two Query blocks:

  1. Query block that displays featured posts (based on tag)

  2. Query block that displays recent posts (except featured posts)

sophiegyo commented 2 years ago

I agree that having a tag filter would be very useful for highlighting special posts on a page (functionality that is similar that Jetpack has for Featured Content).

I was going to add the exact same thing; it'd be very nifty to have a method of excluding a taxonomy from a query loop, to have a few 'featured' posts at the top of a template, that are then not repeated in the query loop below.

padams commented 2 years ago

One additional display option that would simplify a lot of code and pattens would be to have the block show the posts' featured image and title or just the featured image. In the case of attachment post type - the attachment's image.

sb726 commented 2 years ago

Coming from #44041 I would just like to add a vote for custom taxonomy terms (tax_relation??) in the Query Loop. Just to be clear, it is really useful to be able to inherit the Query Loop (for say an index page or category page) AND filter that page according to a custom taxonomy. This was possible (as a bug it seems) in version 6.01. It disappeared in 6.02. I'm hoping we get the functionality back perhaps through this workflow. It means you can do a great deal with category pages.

bradhogan commented 2 years ago

Happy to open a new ticket if necessary. Seems that you cannot create a template for a custom taxonomy then use the query block on that template to display the custom posts that are associated with that tax.

So..

If I have registered let's say Books (cpt) and Genre (custom tax).

I then create a taxonomy-genre.html template with a query:

<!-- wp:query {"queryId":0,"query":{"perPage":12,"pages":100,"offset":0,"postType":"books","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false,"parents":[]},"displayLayout":{"type":"flex","columns":3},"align":"full"} -->

    <div class="wp-block-query alignfull">

        <!-- wp:post-template -->

            <!-- wp:post-featured-image {"isLink":true} /-->
            <!-- wp:post-title {"isLink":true,"style":{"spacing":{"margin":{"top":"var:preset|spacing|50","bottom":"var:preset|spacing|20"}}},"fontSize":"large"} /-->

            <!-- wp:group {"className":"taxonomy-as-buttons","layout":{"type":"flex","flexWrap":"wrap"}} -->
                <div class="wp-block-group taxonomy-as-buttons">
                    <!-- wp:post-terms {"term":"genre","separator":""} /-->
                    <!-- wp:post-terms {"term":"series","separator":""} /-->
                </div>
            <!-- /wp:group -->

            <!-- wp:post-excerpt {"moreText":"","style":{"spacing":{"margin":{"top":"var:preset|spacing|40"}}},"fontSize":"small"} /-->

    <!-- /wp:post-template -->

    <!-- wp:query-no-results --><!-- wp:paragraph {"placeholder":"Add text or blocks that will display when a query returns no results."} --><p>Sorry, no books.</p><!-- /wp:paragraph --><!-- /wp:query-no-results -->

    </div>

<!-- /wp:query -->

Then in the front-end, I click a genre link (let's say "Mystery" as an example), I'm seeing ALL books (the query is not limited the results to just the cpt and custom tax).

ntsekouras commented 2 years ago

👋 @bradley2083 I noticed in your Query Block that you have:

"inherit":false

When you want to use the block in a template you should have the block to inherit from the global query, so it should be true. Please share if you still have issues with the suggested change.

bradhogan commented 2 years ago

@ntsekouras That worked. Thank you!

sb726 commented 1 year ago

@TimothyBJacobs

Could I merge this issue #44041 (which seems to be related), i.e. incorporating full taxonomies into the query block so that filtering is possible across categories and other terms while the query is being inherited from the main search

masteradhoc commented 1 year ago

+1 for the exclude functionality of categories, etc.

supernovia commented 1 year ago

+1 for exclude tags/categories/current post for the single template