Quantcast
Channel: Toolset » All Posts
Viewing all articles
Browse latest Browse all 20145

update_post_meta does not work for custom fields

$
0
0

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.


Viewing all articles
Browse latest Browse all 20145

Trending Articles