• Resolved ash6789

    (@ash6789)


    Hey

    Actually am creating a form to update the post content.

    What I did

    1) created a form for update post
    2) loaded the post content into that form
    3) on submit updated the content of that form

    Issue :
    On submit it also added one post , also updating the post.What I want it just need to update not added one more post.Here my code:

    function update_post_from_car_submission() {
    
        // Get the submitted field values
        $post_title = af_get_field('car_name');
        $post_content = af_get_field('car_description');
        $post_img2 = $_POST['acf']['field_5a8a3a9a526e3'];
        $upostid = af_get_field('field_5a924b672a753');
    
        $model = af_get_field('selected_car_model_is');
        $price = af_get_field('price');
        $variant = af_get_field('selected_variant_is');
        $make = af_get_field('selected_car_make_is');
        $type = af_get_field('type');
        $fueltype = af_get_field('fuel_type');
        $gallery = af_get_field('gallery');
        
        $year = af_get_field('year');
        $post_data = array(
            'ID' => $upostid,
            'post_type' => 'cars',
            'post_status' => 'pending',
            'post_title' => $post_title,
            'post_content' => $post_content,
        );
        $post_id = wp_update_post($post_data);
    
        if ($model) {
            update_field("field_5a6bf9b634f71", $model, $post_id);
        }
        if ($make) {
            update_field("field_5a6c5e6198059", $make, $post_id);
        }
        if ($price) {
            update_field("field_5a59be8db2240", $price, $post_id);
        }
        if ($variant) {
    
            update_field("field_5a6c36711836b", $variant, $post_id);
        }
        if ($type) {
            update_field("field_5a64b0c1a2e8e", $type, $post_id);
        }
        if ($fueltype) {
            update_field("field_5a6c3bc55ee8f", $fueltype, $post_id);
        }
        if ($gallery) {
            update_field("field_5a59be8db22c3", $gallery, $post_id);
        }
        if ($year) {
            update_field("field_5a59be8db21c6", $year, $post_id);
        }
    
        if ($post_img2) {
            update_post_meta($post_id, '_thumbnail_id', $post_img2);
        }
    }   
    
    add_action('af/form/submission/key=form_5a89c16a59701', 'update_post_from_car_submission', 10);

    Also tried acf/save Still same issue.

    So what I need to use so that it just update the value not create one new.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author fabianlindfors

    (@fabianlindfors)

    Hi!

    I can’t see any obvious issues with your code. Are you certain that a new post is created instead of being updated? wp_update_post shouldn’t be able to create a new post. Could you also check the return value of wp_update_post and make sure it’s not WP_Error?

    Plugin Author fabianlindfors

    (@fabianlindfors)

    I’m going to close this now! Please re-open if you still have problems.

    If you are interested I also just released Advanced Forms Pro. The Pro version includes a simple way to create and edit posts which might fit your use case.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Updare Post’ is closed to new replies.