Support

Account

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

  • UPDATE, @niq1982 code doesn’t work if you are using a get_field() where you actually specify the post_id (e.g. get_field(‘field_name’, $somePostID) ).

    I was able to fix this by checking if the $original_post_id is indeed a revision of the $post_id.

    function fix_acf_field_post_id_on_preview($post_id, $original_post_id)
    {
        // Don't do anything to options
        if (is_string($post_id) && str_contains($post_id, 'option')) {
            return $post_id;
        }
        // Don't do anything to blocks
        if (is_string($original_post_id) && str_contains($original_post_id, 'block')) {
            return $post_id;
        }
    
        // This should only affect on post meta fields
        if (is_preview()) {
    		if ( $original_post_id !== $post_id ) {
    			// Check if $post_id is a revision
    			$parent_post_id = wp_is_post_revision( $post_id );
    
    			if ( $parent_post_id === $original_post_id ) {
    				return get_the_ID();
    			}
    		}
        }
    
        return $post_id;
    }
    add_filter('acf/validate_post_id', __NAMESPACE__ . '\fix_acf_field_post_id_on_preview', 10, 2);