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â]));
}
}
You 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.