Storing metadata for custom post types is easy, you just need to add an action for ‘save_post’ with your method that handles this. In which you have then something like the following for a field that you want to store (with the usual security checks of course):
update_post_meta( $post_id, '_description', $_POST['_description'] );
Simple enough. However, I cannot figure out what I need to do so that WordPress stores revisions for a custom post.
I have revisions enabled for the custom post type and revisions work for standard fields like ‘title’. But it doesn’t work by default for metadata.
I’ve looked for how other people do this and almost all give the following code snippet:
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( 'my_custom_post', $post_id, 'my_meta', $my_meta );
}
} add_action( 'save_post', 'my_plugin_save_post' );
But this doesn’t work for me. I don’t see a new revision in the revision listing when I change one of the metadata fields. I do see a new revision when I change for example the title of the custom post.
I’m either not familiar with some detail(s) of how this should be done compared to how I usually store metadata (either due to unfamiliarity with this part of the API or data structures). Or I’m missing something due to this cold that I have.
Has anyone a good example of what you need to do to store revisions for custom post metadata?