7

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?

3
  • 1
    For anyone coming here, the WP-Post-Meta-Revisions Plugin might be a good and more more current starting point. Nov 27, 2016 at 12:32
  • 1
    The first argument for add_metadata is not the post type slug, but the object type, which accepts 'post', 'comment', 'term' or, 'user' . In your case post should be fine even if it's a custom post type. Sep 23, 2022 at 12:53
  • @LeoGermani thank you, I had exactly this problem. I thought I have to use add_metadata as you can specify a different type compared to add_post_meta where this is set to post automatically. But using just post finally made the custom fields appear on my custom post type. Dec 2, 2022 at 8:41

1 Answer 1

2

There's a private filter you can (but shouldn't) access:

Defined in wp-includes/revision.php:

/**
 * Filter the list of fields saved in post revisions.
 *
 * Included by default: 'post_title', 'post_content' and 'post_excerpt'.
 *
 * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date',
 * 'post_date_gmt', 'post_status', 'post_type', 'comment_count',
 * and 'post_author'.
 *
 * @since 2.6.0
 * @since 4.5.0 The `$post` parameter was added.
 *
 * @param array $fields List of fields to revision. Contains 'post_title',
 *                      'post_content', and 'post_excerpt' by default.
 * @param array $post   A post array being processed for insertion as a post revision.
 */
$fields = apply_filters( '_wp_post_revision_fields', $fields, $post );

So you could write a function like

add_filter( '_wp_post_revision_fields', 'add_my_own_fields', 10, 2 );

function add_my_own_fields( $fields, $post ) {
    $fields['meta_field'] = 'Meta Field';
}

Where meta_field is the key for your meta that you want revisioned and 'Meta Field' is what displays as the heading in the revisions UI.

Please note that this is private and it's use is not intended for theme or plugin developers. More elaborate solutions would be needed to avoid this problem, and you may be better off with a plugin that can handle those problems as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.