Make Query block respect perPage
option when sticky posts exist
#41184
Labels
[Block] Query Loop
Affects the Query Loop Block
Needs Decision
Needs a decision to be actionable or relevant
Currently if we have sticky posts, they will be prepended to the results of the query block, regardless of what was set in the
perPage
property.For example, if we had two posts, one "sticky":
And the following query in my homepage:
Intuitively, I would expect to see only one post, but I am shown two:
Why this happens
The Query block is built on top of the
WP_Query
class [WP_Query docs], which is where this behavior originates WP_Query issue marked as won't fix. This is the default behavior and probably won't be changed onWP_Query
, but thecore/query
block does not have to match the interface of WP_Query exactly (and doesn't, in https://github.com/WordPress/gutenberg/pull/38909/files we change the arguments passed to WP_Query to give more intuitive results for"sticky": "only"
)Suggested behavior
There are currently three options for the
sticky
setting.I suggest changing the behavior of the "Include" option (which generates the block markup:
"sticky": ""
). This is also the default behavior if no "sticky" option is set.I suggest changing the
Include
option so that it includes sticky posts first in the list but will respect the perPage option. So a query with<!-- wp:query {"queryId":35,"query":{"perPage":"1", "sticky": "" } } -->
would return a single post. Either the latest stickied post, or, if no sticky posts exist, the latest non-stickied post.Pseudocode implementation
I replaced the
sticky
property handling code inbuild_query_vars_from_query_block
with the following:This change resulted in the behavior I expected, selecting sticky posts first while respecting the "perPage" option. I also chcecked that
order
still works as beforeI was able to test the change by hacking
build_query_vars_from_query_block
inwp-content/blog.php
directly (tested on wpcom ;) ), but it looks like it can be implemented ingutenberg_build_query_vars_from_query_block
in thelib/compat
folder of gutenberg e.g. https://github.com/WordPress/gutenberg/blob/trunk/lib/compat/wordpress-6.0/blocks.php#L37The text was updated successfully, but these errors were encountered: