Open SGr33n opened 2 years ago
đź‘‹ - As I understand it is_main_query doesn't accept any params and checks against the global one, so the above to me seems expected. If you go somewhere around here and check if the actual block's query(from Post Template) is the main one ($query->is_main_query()
), it will return false
.
This issue was reviewed in today's Editor Bug Scrub.
If you go somewhere around here and check if the actual block's query(from Post Template) is the main one ($query->is_main_query()), it will return false.
@SGr33n does this accomplish what you are looking for? If not, can you provide any addition info? Thanks!
Hi @ndiego , thanks for your help and for taking care of this issue. This doesn't accomplish what I'm looking for. The main query should be the one which is obtained by the permalink structure. If I add a block to the single page e.g., a query loop to show the latest post inside the sidebar, in a single post template, the is_main_query() conditional function is returning true, even it's a second query (latest posts), not the main one (single post). This is what my testing filter returns:
Help us move this issue forward. This issue is being marked stale since it has no activity after 15 days of requesting more information. Please add info requested so we can help move the issue forward. Note: The triage policy is to close stale issues that need more info and no response after 2 weeks.
This issue is still present, It's a bug in my opinion, and solving this could help us doing things like showing last posts in a sidebar excluding the post we are viewing.
Thanks.
Help us move this issue forward. This issue is being marked stale since it has no activity after 15 days of requesting more information. Please add info requested so we can help move the issue forward. Note: The triage policy is to close stale issues that need more info and no response after 2 weeks.
Hi @SGr33n - just checking - is this issue still present and relevant in WP6.2
Hi @jordesign ! Thanks for your interest! ATM (WP 6.2.2 / TwentyTwenty Three 1.1) the issue is still present (some differences since when I posted earlier). Loop query blocks are all is_main_query().
https://developer.wordpress.org/reference/functions/is_main_query/
This is the correct behavior for the is_main_query()
function, which checks whether the global $wp_the_query
matches the global $wp_query
. This is pretty much always going to be true
for checks like you have in the code.,
To check whether the current query via the Query Loop block (this is what you're after) is the main query, you need to target the WP_Query
object for block itself. That would be $query->is_main_query()
: https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/post-template/index.php#L68
Unfortunately, it doesn't look like there's a way to access the $query
executed by the Post Template block. So, I'm switching this ticket to an enhancement. The solution may be the addition of a hook there.
Anyway, here's an old Gist I wrote that may be helpful for you for checking if the current query being performed is the actual main query: https://gist.github.com/justintadlock/4551657
Description
The is_main_query() conditional function should return false if running inside an external query loop block.
Step-by-step reproduction instructions
Add the following code to your function, then add a query block to your theme outside the post content.
Screenshots, screen recording, code snippet
No response
Environment info
No response
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes