I have some code that updates a meta-key's value by incrementing...

    $post_id = $wpdb->get_var( $query );
    $meta = get_post_meta( $post_id, 'stuff', TRUE );
    update_post_meta( $post_id, 'stuff', $meta );

I want to create and save a revision of the meta key/ value each time it is updated, so I can determine how many updates took place last week, last month, last year, etc.

I am looking at this code here, but I don't want to do it from the edit post screen, it needs to happen when I update the post meta.

1 Answer 1


You'd need to set up an additional custom field and update it whenever the user changes or updates the original custom field.

  • The problem with this is that I can only save history 1-update deep. I think I need to somehow use revisions. See my updated question
    – marctain
    Jul 18, 2012 at 16:18
  • You could use a serialized array instead of a straight string, and push the new revision time onto the serialized array.
    – Tyrun
    Jul 18, 2012 at 16:50
  • I'm not familiar with serialized arrays.. There could feasibly be around 200 custom fields per custom post (the fields are created dynamically), would I be saving an extra custom field for each of those? Would this serialized array be able to record each and everytime a meta value is updated?
    – marctain
    Jul 18, 2012 at 19:27
  • Yea - ok, so serialized arrays were a big stumper for me as well until I understood them. Basically a serialized array is a multidimensional array (think rows and columns) squashed flat so it fits into a one dimensional cell in a table. Read Nacin's post to understand a little more: nacin.com/2010/04/18/wordpress-serializing-data Yes the serialized array could expand as much as you need it to to store each revision (if you wanted you could even cap it at say 1000 or whatever you wished). You could either store each custom field in a separate history array or in one big giant one.
    – Tyrun
    Jul 19, 2012 at 12:19

Your Answer

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

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