Skip to content

is_main_query is returning true inside query loop blocks. #43922

@SGr33n

Description

@SGr33n

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions