WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.5k stars 4.19k forks source link

is_main_query is returning true inside query loop blocks. #43922

Open SGr33n opened 2 years ago

SGr33n commented 2 years ago

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.

function main_query_show_warning( $block_content, $block ) {
    if ( is_main_query() ) {
        $warning = sprintf(
            '<div style="font-size: 14px; background: red; color: #fff; padding: 2px 6px; font-weight: 700; display: inline-block;">%s</div>',
            __( 'this is a main query' ),
        );

        $block_content = $warning . $block_content; 
    }

    return $block_content;
}

add_filter( 'render_block_core/query', 'main_query_show_warning', 10, 2 );

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

ntsekouras commented 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.

ndiego commented 2 years ago

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!

SGr33n commented 2 years ago

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:

test

github-actions[bot] commented 2 years ago

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.

SGr33n commented 2 years ago

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.

github-actions[bot] commented 2 years ago

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.

jordesign commented 1 year ago

Hi @SGr33n - just checking - is this issue still present and relevant in WP6.2

SGr33n commented 1 year ago

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/

The is_main_query() function is a conditional function that can be used to evaluate whether the current query (such as within the loop) is the “main” query (as opposed to a secondary query).

justintadlock commented 9 months ago

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