Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đang mở
#902 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

@tomjn đang làm issue này rồi.

Từ ngày 2/10/2026.

  • #904 của @tomjn — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
76/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
php
Lĩnh vực
tooling

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.
Ngôn ngữ chính
PHP
Star
261
Fork
44
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Automattic/VIP-Coding-Standards

Tất cả issue của Automattic/VIP-Coding-Standards

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.