Betty
Betty

Reputation: 572

WordPress: get custom field value before a post is updated

What I want to do: In WordPress, when I edit the custom field value of a post, get the old value and the new value, and compare them.

What I get now: I can only get the new value. I hook to the pre_post_update action, and I use get_post_meta() (and a bunch of related functions described in http://codex.wordpress.org/Custom_Fields) to get the value.

I also tried get_post(), which can return the old value of post_content, post_title, etc. But get_post() does not include the custom field values.

I think that at the time when the pre_post_update hook fires, the values have not been saved in the database yet, isn't it? The fact that I can get old value of post title, etc. proves it. Then there must be some way to get the old value of custom fields.

It is also welcomed if you can point out other ways to compare the old value and new value of custom fields.

Many thanks in advance.

Upvotes: 0

Views: 2086

Answers (2)

Pigi
Pigi

Reputation: 74

You can't compare an old custom field with new, because when you update them it will rewritten also in the "wp_post_meta" table in db. You lose the old value for the new.

Upvotes: 1

bingjie2680
bingjie2680

Reputation: 7773

Pigi has made the point. it will be over written. why not use too custom fields with two keys, old Key and new Key, and assign both old value to old key,new value to new key.

Upvotes: 1

Related Questions