Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update wordpress post from front-end (using acf)

I am making front end admin for users to add/edit their posts. I already made post add form and it works, but edit form doesnt work.

functions.php

function add_new_post( $post_id )
{
    if( $post_id == 'new' ) {
        // Create a new post
        $post = array(
            'post_title' => $_POST["fields"]['field_52c810cb44c7a'],
            'post_category' => array(4),
            'post_status'  => 'draft',
            'post_type'  => 'post'
        );

        // insert the post
        $post_id = wp_insert_post( $post );

        return $post_id;
    }
    else {
        return $post_id;
    }
}add_filter('acf/pre_save_post' , 'add_new_post' );

index.php

<div id="updateform-<?php the_ID(); ?>" class="collapse">
    <?php
    echo get_the_ID();
    $args = array(
        'post_id' => get_the_ID(), // post id to get field groups from and save data to
        'field_groups' => array(31), // this will find the field groups for this post (post ID's of the acf post objects)
        'form' => true, // set this to false to prevent the <form> tag from being created
        'form_attributes' => array( // attributes will be added to the form element
            'id' => 'post',
            'class' => '',
            'action' => get_permalink( get_the_ID() ),
            'method' => 'post',
        ),
        'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
        'html_before_fields' => '', // html inside form before fields
        'html_after_fields' => '', // html inside form after fields
        'submit_value' => 'Update', // value for submit field
        'updated_message' => 'Post updated.', // default updated message. Can be false to show no message
    );
    acf_form( $args );
    ?>
</div>
like image 678
Rokas Avatar asked Nov 11 '22 15:11

Rokas


1 Answers

For saving posts you should use save_post not pre_save_post. Basically pre_save_post is used for new posts.

Use this below add_filter('save_post' , 'add_new_post');

like image 105
Ravi Shanker SIPL Avatar answered Nov 15 '22 04:11

Ravi Shanker SIPL