Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

オープン
#902 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

@tomjn がすでに取り組んでいます。

2026年10月2日 から。

  • #904 @tomjn による — オープン

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
php
領域
tooling

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.
主要言語
PHP
スター
261
フォーク
44
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Automattic/VIP-Coding-Standards のほかの issue

Automattic/VIP-Coding-Standards の issue をすべて見る

似ている issue

PHP の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。