Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Bug: PreGetPosts warns when the early is_main_query() return is not the first statement in its if

Abierto
#902 0 comentarios 0 reacciones 0 asignados Ver en GitHub

@tomjn ya está trabajando en esto.

Desde el 2/10/2026.

  • #904 de @tomjn — abierto

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
76/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
php
Área
tooling

Línea de trabajo

Start in PreGetPostsSniff::isEarlyMainQueryCheck(), where the issue identifies the local findNext() search as stopping at the first statement terminator. Reproduce the minimal callback with do_log() before return and confirm the warning on the set() line. Done means the callback produces no warning when the early return is not the first statement in the if body.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Bug Description

The PreGetPosts sniff warns when a pre_get_posts callback changes the query without checking is_main_query() first. A callback that returns early for every other query has made that check, so it should not get the warning.

It does get the warning when the early return is not the first statement inside the if. Logging a message before returning is enough to cause it.

Expected: no warning for the snippet below.

Actual: a warning on the $query->set() line. Remove the do_log() line and the warning goes away.

The cause is in PreGetPostsSniff::isEarlyMainQueryCheck(). It looks for the return with findNext() in local mode, which stops at the first statement terminator, so only the first statement in the if body is searched.

@GaryJones found this while reviewing #901.

Minimal Code Snippet

add_action( 'pre_get_posts', function( $query ) {
	if ( ! $query->is_main_query() ) {
		do_log( 'x' );
		return;
	}
	$query->set( 'cat', '-5' );
} );

Error Code

WordPressVIPMinimum.Hooks.PreGetPosts.PreGetPosts

Main WP_Query is being modified without $query->is_main_query() check. Needs manual inspection.

Environment

Question Answer
PHP version 8.5.11
PHP_CodeSniffer version 3.13.6
PHPCSUtils version 1.2.3
VIPCS version main at 9c47cd0 (3.1.0) and develop at bdd2160
WordPressCS version 3.4.1
PHPCSExtra version 1.5.1
VariableAnalysis version 2.13.0

Tested Against main branch?

  • I have verified the issue still exists in the main branch of VIPCS.
  • I have verified the issue still exists in the develop branch of VIPCS.
Lenguaje dominante
PHP
Estrellas
261
Forks
44
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Automattic/VIP-Coding-Standards

Todos los issues de Automattic/VIP-Coding-Standards

Issues similares

Más issues de PHP

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.