Skip to content

wp:query-loop doesn't dynamically use the chosen category #11

Closed
@bobbingwide

Description

I've implemented the category.html template as far as possible, with the current Gutenberg FSE functionality.

There are a number of problems:

  • the query loop doesn't dynamically use the chosen category.
  • pagination doesn't work at all

Requirement

The Gutenberg blocks wp:query, wp:query-loop and wp:query can be used in templates and template parts to create user defined queries and pagination.

Currently ( Gutenberg 9.2.2 ) they don't support the main query that WordPress has already parsed.

This makes it extremely difficult to create archive pages that support the selected taxonomy term ( category, tags or custom taxonomy ).

I need a simple solution that allows me to process the posts in the main query.

Proposed solution

In the current implementation the wp:query-loop block is nested in the wp:query block.
The wp:post-* blocks are nested within the wp:query-loop.

eg

<!-- wp:query {"queryId":1,"query":{"perPage":2,"pages":1,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[542]}} -->
<!-- wp:query-loop -->
<!-- wp:post-title /-->
<!-- wp:post-featured-image {"isLink":true} /-->
<!-- wp:post-date /-->
<!-- wp:post-excerpt /-->
<!-- /wp:query-loop -->
<!-- /wp:query -->

The query-loop block uses the $block->context passed to it from the query block to construct the query.
When we want to process the main loop we need to execute the query already defined in global $wp_query.

This can be achieved using the following code:

function gutenberg_render_block_core_query_loop_main_query( $attributes, $content, $block ) {
    if ( have_posts() ) {
        $content = '';
        while ( have_posts() ) {
            the_post();
            $post = get_post();
            $content .= (
            new WP_Block(
                $block->parsed_block,
                array(
                    'postType' => $post->post_type,
                    'postId' => $post->ID,
                )
            )
            )->render(array('dynamic' => false));
        }
    } else {
        $content = "No posts found. So why isn't this a 404?";
    }
    return $content;
}

If we remove the outer wp:query block then the $block->context will not be set.
This will prompt us to run the main query.
eg

<!-- wp:query-loop -->
<!-- wp:post-title /-->
<!-- wp:post-featured-image {"isLink":true} /-->
<!-- wp:post-date /-->
<!-- wp:post-excerpt /-->
<!-- /wp:query-loop -->

Steps:

For query-loop:

  • Prototype a solution implemented in the callback function for the dynamic block wp:query-loop; gutenberg_render_block_core_query_loop() in query-loop.php.
  • Raise an issue against Gutenberg. . No need, there's one already: 25377
  • Create a pull request to implement the improvement.

For query-pagination:

  • take a similar approach as for query-loop
  • Raise a separate issue against Fizzie
  • Raise a separate issue against Gutenberg.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

bugSomething isn't workingenhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions