Skip to content
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

Add a filter hook to post template WP_Query or build_query_vars_from_query_block() #36504

Closed
msaari opened this issue Nov 16, 2021 · 6 comments
Closed
Labels
[Block] Post Template Affects the Post Template Block [Feature] Extensibility The ability to extend blocks or the editing experience

Comments

@msaari
Copy link

msaari commented Nov 16, 2021

The Relevanssi search plugin does not work with Query Loop and Post Template blocks. When there's a search results page built using the Query Loop and Post Template blocks, the results come up empty when Relevanssi is active. This is because Relevanssi cannot override the WP_Query used in the Post Template block.

This would be easy to fix by adding a single filter hook somewhere in the process. All Relevanssi needs is the ability to add a relevanssi query variable, set to true.

This could be done either in the Post Template block by changing

$query = new WP_Query( $query_args );

in build/block-library/blocks/post-template.php to

$query = new WP_Query( apply_filters( 'gutenberg_render_block_core_post_template_query_args', $query_args ) );

or in the build_query_vars_from_query_block() function in wp-includes/block.php by changing

return $query;

to

return apply_filters( 'query_vars_from_query_block', $query );

Either way, this would let Relevanssi get in the query and the search would work. There would be no adverse side effects, and I'm sure Relevanssi isn't the only party with an interest in getting to touch the query variables in these processes.

@talldan talldan added [Block] Post Template Affects the Post Template Block [Feature] Extensibility The ability to extend blocks or the editing experience labels Nov 23, 2021
@fabiankaegy
Copy link
Member

This would also solve that is currently it is not possible to extend the Post Template block to add support for custom taxonomies.

Adding the ability to filter the query args and access the attributes would allow anyone to extend the query however they need.

@grappler
Copy link
Member

I have created a trac ticket and have added a patch https://core.trac.wordpress.org/ticket/54850

@grappler
Copy link
Member

As I have started to try and use the new filter, I have realized that attributes from the core\query block are passed individually to the core/post-template block, thus not allowing any new attributes. I was looking to add an include attribute to connect with post__in.

@sc0ttkclark
Copy link

This would also be helpful for me as I was just looking to extend the functionality of the Query Loop block and found it completely lacking in this particular area for me and other developers to customize the query arguments that the function builds up using the WP_Block object context information.

I went through and was about to create a PR until I thought to double check and see if someone had already submitted one. The patch/PR in WordPress/wordpress-develop#2185 does exactly what I'd want to be able to do here which is filter the query arguments based on the block object itself and any context passed to it.

@huubl
Copy link
Contributor

huubl commented Jul 10, 2022

Shouldn't this be changed in Gutenberg repository (too)?

@ntsekouras
Copy link
Contributor

Thanks for the issue @msaari! I'm closing this as this has been implemented here: #43590.

You can read more about extending Query Loop in the related docs page.

Feel free to reopen, if you need something more/different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Post Template Affects the Post Template Block [Feature] Extensibility The ability to extend blocks or the editing experience
Projects
None yet
Development

No branches or pull requests

7 participants