Hacktoberfest 2026: die Issues, die Maintainer für den Oktober markiert haben – offen und einsteigerfreundlich. Hacktoberfest-Issues durchsuchen

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

Offen
#902 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

@tomjn arbeitet bereits daran.

Seit 02.10.2026.

  • #904 von @tomjn — offen

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
76/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
php
Bereich
tooling

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.
Vorherrschende Sprache
PHP
Sterne
261
Forks
44
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus Automattic/VIP-Coding-Standards

Alle Issues in Automattic/VIP-Coding-Standards

Ähnliche Issues

Weitere Issues zu PHP

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.