保存POST Meta的当前版本和新版本

时间:2012-01-04 作者:Marc

我想保存两个名为“侧栏”的自定义元框数据版本。我想稍后比较此数据的新版本和旧版本,因此我想在保存新版本之前,将当前“侧栏”的副本保存到名为“sidebar\\u old”的字段中。以下是我的尝试:

add_action(\'pre_post_update\', \'content_old_save\');

function content_old_save() {
    global $post;
    $sidebar_old = get_post_meta($post->ID, \'sidebar\', true);

    update_post_meta($post->ID, "sidebar_old", $sidebar_old); // current/old sidebar
}
以下是我保存新版本的方式:

add_action(\'save_post\', \'sidebar_title_update\');

function sidebar_title_update($post_id) {
    global $post;

    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) {
    return $post_id;
    }

    update_post_meta($post->ID, "sidebar", $_POST["sidebar"]); // new sidebar
}
问题是,只有新的边栏值被插入到数据库中,“边栏”和“旧边栏”,所以新的值甚至在pre\\u post\\u更新之前就已经插入到数据库中了?

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

这可能是由于保存修订后的内容导致的,您应该使用wp_insert_post_data 无论何时,只要你想在帖子保存之前做点什么,这里有一个我刚刚制作的插件示例来测试它,它看起来像这样:

enter image description here

<?php
/*
Plugin Name: wpse37901
Plugin URI: http://en.bainternet.info
Description: answer to  http://wordpress.stackexchange.com/questions/37901/save-both-current-and-new-version-of-post-meta
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/



add_action(\'save_post\', \'sidebar_title_update\');

function sidebar_title_update($post_id) {
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
        return;

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times

    if ( !wp_verify_nonce( $_POST[\'wpse37901_noncename\'], plugin_basename( __FILE__ ) ) )
        return;


    // Check permissions
    if ( \'post\' == $_POST[\'post_type\'] ) 
    {
        if ( !current_user_can( \'edit_post\', $post_id ) )
            return;
    }else{
        return;
    }

    // OK, we\'re authenticated: we need to find and save the data

    if (isset($_POST[\'sidebar\'])){      
        //save new
        update_post_meta($post_id, "sidebar", $_POST["sidebar"]); // new sidebar
    }
}

add_action( \'add_meta_boxes\', \'wpse37901_add_custom_box\' );

function wpse37901_add_custom_box() {
    add_meta_box( 
        \'wpse37901_sectionid\',
        __( \'wpse37901 test field\'),
        \'wpse37901_inner_custom_box\',
        \'post\' 
    );
}

add_action(\'wp_insert_post_data\',\'wpse37901_store_revision\');

function wpse37901_store_revision($data,$postarr){
    global $post;
    if ($data[\'post_type\'] !== \'revision\' && isset($_POST[\'sidebar\'])){
        //save old
            //get last saved data
        $sidebar_old = get_post_meta($post->ID, \'sidebar\', true);
            //only store if value updated
        if ($_POST[\'sidebar\'] != $sidebar_old){
                //get stored revisions
            $sidebar_old_saved = get_post_meta($post->ID, \'sidebar_old\', true);
                //add last to revisions
            $sidebar_old_saved[] = array(\'date\' => date("j, n, Y  H:i:s"), \'value\' =>$sidebar_old);
                //update revisions array
            update_post_meta($post->ID, "sidebar_old", $sidebar_old_saved);
        }
    }
    return $data;
}

function wpse37901_inner_custom_box( $post ) {
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), \'wpse37901_noncename\' );

    $sidebar_saved = get_post_meta($post->ID, \'sidebar\', true);
    // The actual fields for data entry
    echo \'<label for="sidebar">\';
    _e("For testing");
    echo \'</label> \';
    echo \'<input type="text" id="sidebar" name="sidebar" value="\'.$sidebar_saved.\'" size="25" />\';
    $sidebar_revisions = get_post_meta($post->ID, \'sidebar_old\', true);
    if (!empty($sidebar_revisions)){
        if (is_array($sidebar_revisions)){
            echo \'<ul>\';
            foreach($sidebar_revisions as $r){
                echo \'<li>\'.$r[\'date\'].\' : \'.$r[\'value\'].\'</li>\'; 
            }
            echo \'</ul>\';
        }
    }

}

结束

相关推荐