How to save custom meta only for review data to see only in “Preview Changes”, not the actual post in the front end

你。 提交于 2019-12-12 15:28:28

问题


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:


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.



来源:https://stackoverflow.com/questions/34992629/how-to-save-custom-meta-only-for-review-data-to-see-only-in-preview-changes-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!