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

Context detection for Query block #30369

Closed
ntsekouras opened this issue Mar 30, 2021 · 5 comments
Closed

Context detection for Query block #30369

ntsekouras opened this issue Mar 30, 2021 · 5 comments
Assignees
Labels
[Block] Query Loop Affects the Query Loop Block [Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") [Type] Enhancement A suggestion for improvement.

Comments

@ntsekouras
Copy link
Contributor

ntsekouras commented Mar 30, 2021

We need to find a way to detect the context a Query block 'lives' and handle any settings needed appropriately. By context I mean FSE context (site-editor) or a page/post (post-editor). Moreover is valuable to even know the kind of template we are on FSE context (related: #29438).

Noting that we'll also have to take into account what happens when we are in post-editor and we edit the template from there.

There is an inherit property in Query which defaults to true and with that set, the Query's query for fetching results is inherited from the global query. In a single page there is no much use in having an inherit Query (doesn't fetch anything), but instead a custom one. On the other hand in an archive or index template proper results are shown without taking into account any other previously explicitly set options (like the number of posts).

Some prior art is here: #27972, where templateSlug is passed as context and is used for now in QueryLoop to set the proper postType if we are in an archive template.

@annezazu annezazu added [Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") and removed [Feature] Full Site Editing labels Jul 24, 2023
@jordesign jordesign added the [Type] Enhancement A suggestion for improvement. label Sep 8, 2023
@mikachan mikachan self-assigned this Sep 30, 2024
@richtabor
Copy link
Member

What would be the immediate advantage of this effort?

@felixarntz
Copy link
Member

felixarntz commented Nov 8, 2024

@richtabor When you use a query block e.g. in a page, with inherit: true it will render a query that includes only that same single page - which is pointless. That's because when you visit the page in the frontend, the current WordPress query (which will be used if inherit: true) is for just that page.

In other words: While having inherit: true as a default makes sense for usage in certain templates or template parts, it does not make sense when used within a singular piece of content (e.g. a post or page). That's why context detection would help, it would lead to a smarter default of inherit.

Potentially we could even go further and not even offer the inherit control when using the query block in a singular piece of content. From a UX perspective, that would probably be even better than using inherit: false as default.

@mikachan
Copy link
Member

Thanks for giving all this context, @felixarntz 🙏 The Query Loop block has had some updates recently, with the last major one in #65820. The block will now:

  • Automatically set inherit to true when inserted into non-singular content, such as an archive or category template.
  • Automatically set inherit to false when in singular content, such as a post, page or the 404 template, as this setting is pointless in this context (as you said).
  • When inserted into singular content, the full custom query controls will be shown in the block settings, which makes it more obvious to the user that it's a custom query.
  • When inserted into non-singular content, the query can still be customised as before, by changing the query type to 'custom'.

With all these recent changes, perhaps we could close this issue?

@ntsekouras
Copy link
Contributor Author

Okay, let's close this one. Thank you!

@tcmulder
Copy link

tcmulder commented Dec 18, 2024

Is it possible to override this, with a filter or something? I have an existing site that used inherit=true on a page, and that page's content got output on archives like tags and categories (allowing me add blocks above and below the core/query that appear on all blog, tag, and category pages) It was working great, inheriting the template's query appropriately.

I understand FSE would be the way to go in the future, but it'd be a huge lift to convert this site over to FSE.

I tried the following, but inherit is always equal to false on the front end for me now:

add_filter( 'render_block_core/query', 'query_block_inherit', 10, 2 );
function query_block_inherit( $block_content, $block ) {
    if ( isset( $block['blockName'] ) && 'core/query' === $block['blockName'] ) {
		$block['attributes']['inherit'] = true;
    }
    return $block_content;
}

EDIT: I also tried the following as it matches $block's structure, but this also doesn't work.

add_filter( 'render_block_core/query', 'query_block_inherit', 99, 2 );
function query_block_inherit( $block_content, $block ) {
    $block['attrs']['query']['inherit'] = true;
    return $block_content;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Query Loop Affects the Query Loop Block [Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") [Type] Enhancement A suggestion for improvement.
Projects
Development

No branches or pull requests

7 participants