Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcaskill/3812b96b7795c3d31c36ad542f454ce9 to your computer and use it in GitHub Desktop.
Save mcaskill/3812b96b7795c3d31c36ad542f454ce9 to your computer and use it in GitHub Desktop.
WP / ACF: Ensure ACF fields can be previewed when post revisions are disabled.
<?php
/**
* Ensure ACF fields can be previewed when post revisions are disabled
* (`WP_POST_REVISIONS`).
*
* @listens action:wp_revisions_to_keep
* Filters the number of revisions to save for the given post.
*/
add_filter( 'wp_revisions_to_keep', function ( int $num, WP_Post $post ) : int {
// Bail early if post revisions enabled.
if ( WP_POST_REVISIONS ) {
return $num;
}
// Bail early if the post type is not supported.
if ( ! post_type_supports( $post->post_type, 'revisions' ) ) {
return $num;
}
// Bail early if not previewing post.
if (
! is_preview() ||
empty( $_GET['preview_id'] )
) {
return $num;
}
$preview_id = (int) $_GET['preview_id'];
// Bail early if not previewing this post.
if ( $preview_id !== $post->ID ) {
return $num;
}
// Bail early if not currently filtering the post ID
// for ACF to fetch from its latest revision.
if (
! doing_filter( 'wp_revisions_to_keep' ) ||
! doing_filter( 'acf/validate_post_id' )
) {
return $num;
}
return 1;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment