カスタムフィールドの値もプレビューに反映
参考:wordpress smart custom fieldがプレビューで表示されない不具合
functions.phpに
function get_previewId($postId) { global $post; $previewId = 0; if($post->ID == $postId && is_preview() && $preview = wp_get_post_autosave($post->ID)) { $previewId = $preview->ID; } return $previewId; } function get_preview_postmeta($return, $postId, $meta_key, $single) { if($previewId = get_previewId($postId)) { if($postId != $previewId) { $return = get_post_meta($previewId, $meta_key, $single); } } return $return; } add_filter('get_post_metadata', 'get_preview_postmeta', 10, 4); function save_preview_postmeta($postId) { global $wpdb; if(wp_is_post_revision($postId)) { $wpdb->query("DELETE FROM $wpdb->postmeta WHERE postId = $postId"); $post_metas = apply_filters('preview_post_meta_keys', array('meta')); foreach($post_metas as $post_meta) { foreach($_POST[$post_meta] as $meta_id => $meta_arr) { add_metadata('post', $postId, $meta_arr['key'], $meta_arr['value']); } } do_action('save_preview_postmeta', $postId); } } add_action('wp_insert_post', 'save_preview_postmeta');
もしくは
function get_previewId($postId) { global $post; $previewId = 0; if($post->ID == $postId && is_preview() && $preview = wp_get_post_autosave($post->ID)) { $previewId = $preview->ID; } return $previewId; } function get_preview_postmeta($return, $postId, $meta_key, $single) { if($previewId = get_previewId($postId)) { if($postId != $previewId) { $return = get_post_meta($previewId, $meta_key, $single); } } return $return; } add_filter('get_post_metadata', 'get_preview_postmeta', 10, 4); function save_preview_postmeta($postId) { global $wpdb; if(wp_is_post_revision($postId)) { if(isset($_POST['fields']) && count($_POST['fields']) != 0) { foreach($_POST['fields'] as $key => $value) { $field = get_field($key); if(!isset($field['name']) || !isset($field['key'])) continue; if(count(get_metadata('post', $postId, $field['name'], $value)) != 0) { update_metadata('post', $postId, $field['name'], $value); update_metadata('post', $postId, "_" . $field['name'], $field['key']); } else { add_metadata('post', $postId, $field['name'], $value); add_metadata('post', $postId, "_" . $field['name'], $field['key']); } } } do_action('save_preview_postmeta', $postId); } } add_action('wp_insert_post', 'save_preview_postmeta');