admin管理员组文章数量:1296441
Lets say I have custom field on post editor, and I change value from AAA to ZZZ.. :
add_action('save_post',
function($post){
$value = get_post_meta($post->ID, 'mykey');
}
, 1);
How to get the old value (AAA) of that meta-key? during save_post (even earlier 1st priority), I get ZZZ
Lets say I have custom field on post editor, and I change value from AAA to ZZZ.. :
add_action('save_post',
function($post){
$value = get_post_meta($post->ID, 'mykey');
}
, 1);
How to get the old value (AAA) of that meta-key? during save_post (even earlier 1st priority), I get ZZZ
Share Improve this question edited Feb 27, 2017 at 9:16 T.Todua asked Feb 26, 2017 at 18:25 T.ToduaT.Todua 5,8609 gold badges52 silver badges79 bronze badges3 Answers
Reset to default 2
save_post
Runs whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email. Action function arguments: post ID and post object. Runs after the data is saved to the database.
above paragraph is quoted from WP Codex.
so you cannot use this hook to get older value because it fires after saving new values to DB. WP has another action hook named wp_insert_post
but sadly this hook does same thing as save_post
alternatively you can use Filters to get the job done. WP provides few filter to edit the post while saving or before saving to DB. like wp_insert_post_data
& content_save_pre
might work for you, i think.
Update
here is another discussionon this topic which might be helpful for you.
The trick I did was:
1) Created a hidden meta box, where I inserted input, with value of current_meta_value
2) during save_post
i checked it against to new_meta_value
.
that was all.
This might be against the WordPress-rules or something. But I got this working:
add_action( 'save_post', 'wp258055_save_post_callback' );
function wp258055_save_post_callback( $post_id ){
$value_in_db = get_post_meta( $post_id, 'mykey', true );
$value_about_to_be_saved = $_POST['mykey'];
}
本文标签: get post meta before it is updated (during SAVEPOST)
版权声明:本文标题:get post meta before it is updated (during SAVE_POST) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741610013a2388201.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论