3
votes

I am trying to add custom meta and see at "Preview Changes". I can see the changes but also changes apply to the actual post at Front end. I want the changes will update to the actual post when it Publish or Update not at "Preview Changes" click. Please help. I have followed this plugin.

function my_plugin_save_post( $post_id, $post ) {

if ( $parent_id = wp_is_post_revision( $post_id ) ) {

    $parent  = get_post( $parent_id );
    $my_meta = get_post_meta( $parent->ID, 'my_meta', true );

    if ( false !== $my_meta )
          add_metadata( 'post', $post_id, 'my_meta', $my_meta );
}

} add_action( 'save_post', 'my_plugin_save_post' );

1

1 Answers

0
votes

The following code will prevent your meta data from saving on preview but You won't be able to preview published posts with your metadata. Honestly I'm trying to figure out this situation myself :/

<?php // In your save metabox data function, near the top...
if (isset( $_POST['wp-preview'] ) && 'dopreview' == $_POST['wp-preview'] ) {
  if(get_post_status($post_id) == 'publish'){
    return; // This way we can still preview draft / scheduled posts
  }
}

Honestly, I'd use this code and set your post briefly to draft or private while editing / previewing and publish them as normal when you're done.