I would like to, when saving or updating a post-type run a function in the functions.php file.
I do succed with running the function, but I do have a problem when I would like to save a post meta value.
The post-meta value is important since I would like to change it later on for my purpose, but for the moment I just want to save a text in the meta value.
When saving the post, then the post meta value is not changed, so I saved the post again, and directly after that I checked at the database, then I could see the new value post meta value which was:
'new post meta value'
but when the post was fully loaded after saving the post, it got the old value from the database.
What am I doing wrong?
Please help me, perhaps someone could try this and help me.
here is the code:
function save_metadata($post_id) {
global $post;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
$new = 'new post meta value';
update_post_meta($post_id, 'wpcf-extra-description', esc_attr($new)) ;
}
add_action( 'save_post', 'save_metadata');
I experimented some nad found out that meta keys created with Types plugin does not save properly in the database.
If I use the code above on a custom fields created by other plugins or manually then the update_post_meta works without problem.
Does someone know how I could fix this issue?
Thanks for all kind of assistance.
Hi amanV,
Please try modify your php code as this:
...
add_action( 'save_post', 'save_metadata', 100);
Types use save_post action hook when save posts, you can apply it after Types
Thanks so much,
that did work..
what exactly does 100 mean in this case?
hi!
please, help.
i inserted this code in function.php
function save_metadata($post_ID) {
update_post_meta(56, 'wpcf-single-site', 'new post meta value');
}
add_action( 'save_post', 'save_metadata' , 100 );
and next i updated post #56 and field "wpcf-single-site" does not changed