5

In the recent wordpress.org article outlining the new Query Loop block, it says the "Inherit query from template" option allows you to "customize the query that the loop relies upon ... WordPress will otherwise rely on the template being used to determine what posts appear".

There appears to be no further guidance on this. What does this mean, and where does it find a query for these posts?

If I have a custom taxonomy defined, how would I pull from that taxonomy rather than the standard categories?

1 Answer 1

10

Update 2: Since 6.1. the query_loop_block_query_vars filter is available if one needs to modify the underlying WP_Query arguments (e.g. those that are still unsupported by the query block's interface) but it only works for the front-end but not the editor preview that uses the REST API (see more on that in the docs here).

Update 1: It looks like support for custom taxonomies filtering will come to version 6.0. Should also be shipped with Gutenberg 12.5 according to:

https://github.com/WordPress/Documentation-Issue-Tracker/issues/283

The below work-around should still work for other more complex types of custom filtering.


Looking into render_block_core_post_template() we can see it calls build_query_vars_from_query_block() (previously named construct_wp_query_args) to setup the query arguments of WP_Query from the Query` block properties.

From there I don't see it supporting custom taxonomies for the secondary query ... yet!

Work-around-idea: For the Query Loop:

enter image description here

add a search keyword for using custom taxonomies, e.g. :query-motor-electric:

enter image description here

and write a plugin to handle this:

// Replace :query-motor-electric search keyword for a custom taxonomy query.
add_action( 'pre_get_posts', function( \WP_Query $q ) {
    if ( $q->is_search() && ':query-motor-electric' === trim( $q->get( 's' ) ) ) {
        // Custom taxonomy query.
        $tax_query = array(
            array(
                'taxonomy' => 'motor',
                'field'    => 'slug',
                'terms'    => 'electric',
            ),
        );
        $q->set( 'tax_query', $tax_query );

        // Clear search, unset search query variable or use a stop-word filter.
        $q->set( 's', '' );
    }
} );

or extend this further to support a dynamic keyword.

Example Block code for the Query Loop:

<!-- wp:query {"queryId":1,"query":{"perPage":3,"pages":1,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":":query-motor-electric","sticky":""}} -->
<div class="wp-block-query"><!-- wp:post-template -->
<!-- wp:post-title /-->

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

<!-- wp:post-excerpt /-->
<!-- /wp:post-template --></div>
<!-- /wp:query -->

where the relevant search part is:

<!-- wp:query {...,"query":{...,"search":":query-motor-electric"}} -->
4
  • thanks so much. This will take me some time to implement as my skills are still quite basic...but thanks to kind folks like you I'm learning quickly :) Aug 6, 2021 at 19:24
  • you're welcome, best of luck with it
    – birgire
    Aug 6, 2021 at 21:29
  • @birgire How would you modify that WP_Query via your theme or plugin for the build_query_vars_from_query_block() function? Nov 10, 2021 at 17:46
  • e.g. override the render callback render_block_core_post_template with a wrapper of the original callback that contains custom hook that can be referenced in the WP_ Query hooks
    – birgire
    Nov 11, 2021 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.