I'm reading a book about WordPress and I'm new and confused.
Why does the author always not save data in the metadata box when its a revision. just in case its not clear, what i refer to by metadata box is the one added by add_meta_box
.
//save meta box data
function pp_save_meta_box($post_id,$post) {
// if post is a revision skip saving our meta box data
if($post->post_type == 'revision') { return; }
// process form data if $_POST is set
if(isset($_POST[’pp_sku’]) && $_POST[’pp_sku’] != ‘’) {
// save the meta box data as post meta using the post ID as a unique prefix
update_post_meta($post_id,’pp_sku’, esc_attr($_POST[’pp_sku’]));
update_post_meta($post_id,’pp_price’, esc_attr($_POST[’pp_price’]));
update_post_meta($post_id,’pp_weight’, esc_attr($_POST[’pp_weight’]));
update_post_meta($post_id,’pp_color’, esc_attr($_POST[’pp_color’]));
update_post_meta($post_id,’pp_inventory’,esc_attr($_POST[’pp_inventory’]));
}
}
I'm reading a book about WordPress and I'm new and confused.
Why does the author always not save data in the metadata box when its a revision. just in case its not clear, what i refer to by metadata box is the one added by add_meta_box
.
//save meta box data
function pp_save_meta_box($post_id,$post) {
// if post is a revision skip saving our meta box data
if($post->post_type == 'revision') { return; }
// process form data if $_POST is set
if(isset($_POST[’pp_sku’]) && $_POST[’pp_sku’] != ‘’) {
// save the meta box data as post meta using the post ID as a unique prefix
update_post_meta($post_id,’pp_sku’, esc_attr($_POST[’pp_sku’]));
update_post_meta($post_id,’pp_price’, esc_attr($_POST[’pp_price’]));
update_post_meta($post_id,’pp_weight’, esc_attr($_POST[’pp_weight’]));
update_post_meta($post_id,’pp_color’, esc_attr($_POST[’pp_color’]));
update_post_meta($post_id,’pp_inventory’,esc_attr($_POST[’pp_inventory’]));
}
}
Share
Improve this question
edited Aug 22, 2020 at 22:52
Pikamander2
6287 silver badges20 bronze badges
asked Sep 11, 2010 at 14:03
Jiew MengJiew Meng
1,9115 gold badges24 silver badges40 bronze badges
2 Answers
Reset to default 5You omitted how is this function called. I assume it is added to save_post
action.
This action passes current post id as argument. In case of revision that would be revision id and not parent post id. So, as I see it, there is no reason to save additional data for revision (creating duplicate set of it).
Update.
Scratch that.
I looked through source code. Apparently *_post_meta
functions will automatically change to parent post id if passed revision post id. So you might modify original post, thinking you are modifying revision.
When I look at my postmeta
table, it appears no metadata is ever saved for post revisions. I don't think it has to be this way: I can think of some use cases where it would be useful for plugins to save additional metadata for revisions too. Take your example, probably from a webshop example: if I first create a product with a price of $20, then later reduce the price to $10, this will probably save the old text fields as a revision, but it will erase the old price fields. It could be useful to save a history of the prices too.
I assume the functions that deal with metadata don't fully support working with revisions of metadata, so we should not expect much support from plugins yet. But, if you create a plugin, keep in mind how saving (and retrieving) historical metadata could be useful.