Skip Navigation

[Gelöst] update_post_meta does not work for custom fields

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created vor 10 Jahre, 11 Monate. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 Antworten, has 3 Stimmen.

Last updated by alexB-11 vor 9 Jahre, 6 Monate.

Assisted by: Luo Yang.

Author
Artikel
#98310

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.

#98653

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

#98755

Thanks so much,
that did work..
what exactly does 100 mean in this case?

#252137

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

Das Forum „Types Community Support“ ist für neue Themen und Antworten geschlossen.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.