Hi !
After an half day of research, we found a solution for preview with post meta (custom fields).
The problem :
If you change a post meta value but not the title, the content or other native fields, the post (or page) doesn’t change for WP…
And when you press the preview button, if there is an autosave, WP delete it because the autosave and the saved post (or page) are the same (title, content,… haven’t change).
I don’t know why WP do that like that but we have the solution (put this code on your function.php file):
/*
Debug preview with custom fields
*/
add_filter('_wp_post_revision_fields', 'add_field_debug_preview');
function add_field_debug_preview($fields){
$fields["debug_preview"] = "debug_preview";
return $fields;
}
add_action( 'edit_form_after_title', 'add_input_debug_preview' );
function add_input_debug_preview() {
echo '<input type="hidden" name="debug_preview" value="debug_preview">';
}
The first hook add a field to check for revisions (debug_preview).
And the second add an hidden field (debug_preview) on edit pages with a value (‘debug_preview’ but any value works).
When WP check for revision, the field ‘debug_preview’ doesn’t exist in database but exist with a value in the edit page, so it’s different and WP make the autosave.
Say “hello back” to preview in WP with post meta :)
I hope this code can help you !
If a WP dev see that, please update the core to autosave each time the preview button is press ;)
Have a nice day !