WordPress: update_post_meta not working

I am modifying an existing plugin and I want to add a new field to the form and then have that field be submitted along with the post. The post gets submitted to wp_posts. I have read on Google that to do this one simply needs to use update_post_meta. I am trying to insert data into a new column I made in PHPMyAdmin. I named the column post_amount. The field name is amount_field. Although I’m a beginner, something about “just use update_post_meta” that I’ve read seems too simple to be all that I need. But I might be wrong. Maybe I’m using it wrong?

Note – this whole attempt is due to me wanting to create a new column in the wp_posts table and add data to it with each posts. Is this even the correct way to do this? I see the words “meta_key” and “meta_value” and it makes me think that this will actually end up adding data in the wp_postmeta table….or is that the intended destination?

$question_array = array(
            'post_title'        =>  $fields['title'],
            'post_author'       =>  $user_id,
            'post_content'      =>  apply_filters('ap_form_contents_filter', $fields['description']),
            'post_type'         =>  'question',
            'post_status'       =>  $status,
            'comment_status'    =>  'open',
        );

        if(isset($fields['parent_id']))
            $question_array['post_parent'] = (int)$fields['parent_id'];

        $question_array = apply_filters('ap_pre_insert_question', $question_array );

        $post_id = wp_insert_post($question_array);

        $post_amount = $fields['amount_field']; //My code

        update_post_meta($post_id, 'post_amount', $post_amount); //My code

Related posts

1 comment

  1. I think you made a mistake updating your post_id.

    In update_post_meta($post_id, 'post_amount', $post_amount);

    you don’t have ID of the post or the ID you want to update.
    $post_id parameter is declared to Insert Post in your above code.
    $post_id = wp_insert_post($question_array); So, update post query didn’t find the ID.

    You need ID to update post meta. It didn’t find any id so it didn’t update meta_key. SORRY FOR MY ENGLISH.

    Try using add_post_meta instead of update_post_meta.

    See the Reference

Comments are closed.