Support

Account

Home Forums Front-end Issues Front End Form Values Staying Filled

Solved

Front End Form Values Staying Filled

    • graphix

    • December 20, 2013 at 3:58 am

    Hello,

    I have an ACF Form which is setup to post a new CPT as a draft once it is submitted and this currently works. However, if I return the the same page where my form was, all of the fields are still filled will the info I last filled out.

    For Example: I fill out the form with the title field filled out as “Test”, a new post is then created (in the admin, set to draft status) with the Title of “Test”. I return to my form to submit another post, but the form’s title field still has the Title of “Test” filled out.

    Is there any way to allow the front end form to be continually used to post new posts, instead of just updating the one?

    I’m displaying the form like this in page.php:

    $args = array(
    	'post_id' => 'new',
    	'field_groups' => array( 4137 )
    );
    				 
    acf_form( $args );

    I’m creating the form like this in functions.php:

    function my_pre_save_post( $post_id )
    {
        // check if this is to be a new post
        if( $post_id != 'new' )
        {
            return $post_id;
        }
      
        // Create a new post
        $post = array(
            'post_status'  => 'draft' ,
            'post_title'  => 'Test' ,
            'post_type'  => 'resource_link' ,
        ); 
      
        // insert the post
        $post_id = wp_insert_post( $post );
    
        // update $_POST['return']
        $_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
      
        // return the new ID
        return $post_id;
    }
      
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    • Elliot

    • December 23, 2013 at 8:59 am

    Hi @graphix

    Can you check your wp_options table for any rows where the option_name starts with ‘new_’ or ‘options_new_’.

    It is possible that during testing, your code saved it’s values with a post_id of ‘new’ and now your post is loading from ‘new’.

    If the rows exist in the DB. Please remove them, and your form should clear out.

    Thanks
    E

    • graphix

    • January 8, 2014 at 3:08 am

    Hi Elliot,

    That did it. I have two rows in my wp_options table (as I was testing with two fields). I deleted both rows and my form now submits correctly!

    Also, I’m not sure if I should start a new topic or not, but is there a way to change the post_title to pull the value from a field that has been setup? I tried this, but it did not work:

    'post_title' => $_POST["fields"]['page_title']

    Where page_title is the name of the field I setup in ACF.

    Thanks very much,

    James

    • graphix

    • January 8, 2014 at 3:14 am

    Nevermind I figured it out,

    I looked at the name of the field in the front end and added it to my code like this and my Custom Post Types now have their custom title:

    'post_title' => $_POST["fields"]['field_52b2ffdfdcad1']

    James

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.