update custom post custom field value when post is saved in wp-admin

How to update custom post custom field value when post is saved in admin?

I have tried to use this in misc.php for admin section:

Read More
add_action('pre_post_update', 'do_something_with_a_post');
function do_something_with_a_post($id) {

    global $post;
    update_post_meta($id, 'ct_Course_Dur_text_d19c', 'test12');

)

But it is not working.

Related posts

Leave a Reply

1 comment

  1. You may try this (Using save_post hook), paste this code in your functions.php file

    function save_cpt_metadata($id, $post)
    {
        if($post->post_type != 'your_custom_post_type') {
            return;
        }
        update_post_meta($id, 'ct_Course_Dur_text_d19c', sanitize_text_field( $_POST['your_custom_field'] ) );
    }
    add_action('save_post', 'save_cpt_metadata');
    

    In this example sanitize_text_field( $_POST['your_custom_field'] ) is would be actually the cstom field on your form but you may use any hard coded data, replace the your_custom_post_type with your real custom post type.