is_preview was called incorrectly notice on every early get_field() since 6.8.10 (acf_revisions::acf_validate_post_id)

未关闭 适合新手
#1,041 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@faisalahammad 已经在做这个了。

开始于 2026年10月12日。

  • #1049 来自 @faisalahammad —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
php
领域
backend

调研方向

从 includes/revisions.php 中的 acf_revisions::acf_validate_post_id() 开始,检查其 acf/validate_post_id hook 和早期 preview 检查。在启用 WP_DEBUG 的情况下复现 plugins_loaded 的 get_field() 调用,然后验证早期读取不再记录 is_preview 通知,同时正常的 preview 验证仍保持完整。

由索引模型根据 Issue 内容生成。

描述

is_preview was called incorrectly notice on every early get_field() since 6.8.10

Environment
  • ACF PRO 6.8.10
  • WordPress 7.0 (multisite)
  • PHP 8.4
  • WP_DEBUG enabled
Description

Since 6.8.10, any get_field() / get_option()-style ACF read made before WordPress creates the main query triggers a PHP notice:

PHP Notice: Function is_preview was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. (This message was added
in version 3.1.0.) in wp-includes/functions.php on line 6131

The notice fires on every such call, so production logs fill up quickly. It also breaks sites that convert notices to exceptions: with Whoops in development, the request ends in
a 500.

Cause

acf_revisions::acf_validate_post_id() (includes/revisions.php) is hooked to acf/validate_post_id, so it runs on every field read. In 6.8.10 its early bail changed:

- // bail early if no preview in URL
- if ( ! isset( $_GET['preview'] ) ) {
+ // Bail early if not a well-formed WordPress preview context.
+ if ( ! is_preview() ) {
      return $post_id;
  }

is_preview() calls _doing_it_wrong() when the global $wp_query is not set yet. WordPress only creates it in wp-settings.php after plugins_loaded. Reading options while
plugins load (settings, feature flags, service bootstrapping) is a common and legitimate pattern, and before 6.8.10 it produced no notice.

Steps to reproduce
  1. Enable WP_DEBUG and WP_DEBUG_LOG.

  2. Add to any plugin:

    add_action( 'plugins_loaded', function () {
        get_field( 'any_field', 'option' );
    } );
    
  3. Load any front-end page.

Expected

No notice, as in 6.8.9 and earlier.

Actual

The is_preview was called incorrectly notice is logged on every call.

Suggested fix

Before the main query exists, is_preview() returns false anyway, so bailing out without calling it keeps the behavior identical:

global $wp_query;

if ( ! isset( $wp_query ) || ! is_preview() ) {
    return $post_id;
}
Workaround

Right after ACF loads, we replace ACF's callback on acf/validate_post_id with a wrapper. The wrapper calls acf()->revisions->acf_validate_post_id() only when
$GLOBALS['wp_query'] is set.

主要语言
PHP
星标
948
派生
197
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

AdvancedCustomFields/acf 的其他 Issue

查看 AdvancedCustomFields/acf 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。