Support

Account

Home Forums General Issues Custom Fields on post Preview Reply To: Custom Fields on post Preview

  • TL;DR: Use the long code block half-way down to fix this.

    I was stuck on this issue for a while. Jann’s solutions above are good, but even with edits some fields broke; they’d always work in some places, but not others. I think the problem with the previous solutions is that, by hooking into acf/pre_load_post_id and short-circuiting it with a fixed post_id, you’re preventing the native ACF code from locating the correct post ID via its function acf_get_valid_post_id.

    Finally, I found the actual cause of this issue. It’s called from the end of that acf_get_valid_post_id function, via the filter acf/validate_post_id (in includes/api/api-helpers.php)

    This filter calls the class method acf_revisions::acf_validate_post_id (which is found in includes/revisions.php — search for acf_validate_post_id).

    The problematic code in acf_validate_post_id starts with the variable $revision on line 377, and ends at line 384 (ACF PRO v5.11) — comment out that block, and previews work correctly. See the attached screenshot for this in context.

    So, the solution is to stop that block of code from running, by removing the filter which runs it:

    // Fix a long-standing issue with ACF, where fields sometimes aren't shown
    // in previews (ie. from Preview > Open in new tab).
    if ( class_exists( 'acf_revisions' ) )
    {
    	// Reference to ACF's <code>acf_revisions</code> class
    	// We need this to target its method, acf_revisions::acf_validate_post_id
    	$acf_revs_cls = acf()->revisions;
    
    	// This hook is added the ACF file: includes/revisions.php:36 (in ACF PRO v5.11)
    	remove_filter( 'acf/validate_post_id', array( $acf_revs_cls, 'acf_validate_post_id', 10 ) );
    }

    Removing the filter works in all the use cases I tried, although I’ve only tested in a Gutenberg environment, and haven’t checked if it works with the Classic Editor plugin yet.

    It’s a somewhat nuclear fix, as it removes the other code related to previews — but it doesn’t prevent non-preview code from running, like filtering acf/pre_load_post_id does.

    I think this is a core ACF bug that needs addressing.

    Also, when the code mentioned above was added in 5.5.10, I don’t think the code should have been put into the filter acf/validate_post_id — in the version before that (5.5.9), that filter wasn’t used internally, and currently, it’s used exclusively for previews (bailing immediately if preview is not in $_GET). So if it needs to stay in ACF core, it should be moved to a method that’s more aptly named.

    Notes for posterity:

    The whole filter code was added in 5.5.10. The changelog mentions an issue with previews:

    Core: Fixed bug preventing values being loaded on a new post/page preview

    Before 5.5.10, in version 5.5.9, the filter acf/validate_post_id wasn’t used internally, with extra code being used in the function acf_get_valid_post_id (in api/api-helpers.php) instead. See the second screenshot for the old code.