Support

Account

Home Forums Bug Reports Preview with ACF fields are incorrect Reply To: Preview with ACF fields are incorrect

  • CamiloAPR’s solution does indeed cause other ACF contexts to stop working, such as options, ACF blocks, etc.

    Here’s my solution that simply checks to see if the $post_id being passed in is for a “single” post along with is_preview() and otherwise doesn’t attempt to intervene:

    
    /**
     * Fix ACF post ID when previewing draft posts.
     *
     * In preview mode, WordPress uses the latest revision/autosave ID, which may not contain the updated ACF meta field data.
     *
     * This support thread got me halfway there, but it had to be modified to only interfere if the post ID is a single post, otherwise fields inside ACF blocks and other contexts don't work.
     * https://support.advancedcustomfields.com/forums/topic/preview-solution/page/3/#post-134967
     *
     * @param null|int $null    The value to return. If null, then the ACF plugin will figure out the context itself.
     * @param int      $post_id The "post ID" to check. Can be a post, but can also be any number of other ACF contexts.
     */
    function fix_acf_post_id_on_preview( $null, $post_id ) {
    	// Only intervene if the post_id is a single post and we're in preview mode.
    	if ( is_single( $post_id ) && is_preview() ) {
    		// Get the actual post ID instead of the current revision ID.
    		return get_the_ID();
    	}
    
    	return $null;
    }
    add_filter( 'acf/pre_load_post_id', 'fix_acf_post_id_on_preview', 10, 2 );