Support

Account

Home Forums Front-end Issues Using acf_form to create a new post

Solved

Using acf_form to create a new post

  • Hello everyone,

    I recently try to use acf_form() to create a front-end submission (using this tutorial: http://www.advancedcustomfields.com/resources/tutorials/using-acf_form-to-create-a-new-post/), but when I put 'post_id' => 'new' in the $args array(), it’s just don’t work…and the front-end form disappear.

    Here’s my code:

    function formation_pre_save_post($post_id) {
      if($post_id != 'new')
        return $post_id;
    
      $post = array(
        'post_status' => 'draft',
        'post_title' => 'FTF',
        'post_type' => 'formation',
      );
    
      $post_id = wp_insert_post($post);
      $_POST['return'] = add_query_arg(array('post_id' => $post_id), $_POST['return']);
      
      return $post_id;
    }
    add_filter('acf/pre_save_post' , 'formation_pre_save_post');
    acf_form_head();
    get_header();
    the_post();
    $args = array(
            // 'post_id' => 'new',
            'submit_value' => __('Create post', 'textdomain'),
            'updated_message' => __('Post created', 'textdomain'),
          );
    
          acf_form($args);

    Thanks 🙂

    Edit: And when I’m try to use 'fields_group' => array(2213), it’s just output Warning: array_merge(): Argument #1 is not an array in C:\XAMPP\htdocs\communicacteurs\wp-content\plugins\advanced-custom-fields\core\api.php on line 1185

  • i have the same problem here.

    i created a custom post type “datensatz” and also used the scripts from this site: http://www.advancedcustomfields.com/resources/tutorials/using-acf_form-to-create-a-new-post/

    i created a page with a template like this:

    <?php acf_form_head();
    get_header(); ?>
     
    	<div id="primary" class="content-area">
    		<div id="content" class="site-content" role="main">
    		
    			<?php /* The loop */ ?>
    			<?php while ( have_posts() ) : the_post(); ?>
     
    				<?php acf_form(array(
    					'post_id'	=> 'new',
    					'submit_value'	=> 'Neuen Datensatz erstellen'
    				)); ?>
     
    			<?php endwhile; ?>
    			 
    		</div><!-- #content -->
    	</div><!-- #primary -->
     
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    and added the following code in my 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'  => 'datensatz',
        );
     
        // 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' );

    on the page in the frontend just the submit button with my custom text shows up, but the form is missing.

    i would really appreciate your support. thank you!

  • I am having the same problem when trying to show a form for creating a new post. I only see the submission button, nothing else. Editing a specific form works.

    I have copied the code exactly as is shown on this page: http://www.advancedcustomfields.com/resources/functions/acf_form/

    I get this error:
    Notice: Undefined index: new_post in /wp-content/plugins/advanced-custom-fields/core/api.php on line 1185 Warning: array_merge(): Argument #1 is not an array in /wp-content/plugins/advanced-custom-fields/core/api.php on line 1185

    Update:
    Using the fix in this post – http://www.advancedcustomfields.com/resources/functions/acf_form/ the error message is removed, but my form is still not showing. Any ideas?

    UPDATE 2!
    I was using the documentation for V5… So I had the wrong code!

  • For v4 and below, you’d need to add in:

    'field_groups' => array(ACF_POST_NUMBER),

    to the ‘acf_form’ array, making sure to check what your ACF post number is and replacing ACF_POST_NUMBER with it.

    Eg.

    <?php acf_form(array(
    'field_groups' => array(5),
    'post_id'	=> 'new',
    'submit_value'	=> 'Neuen Datensatz erstellen'
    )); ?>
  • My mistake, it’s still not function.

    $args = array(
                'fields_group' => array(2213),
                'post_id' => 'new',
                'submit_value' => __('Submit formation', 'communic'),
                'updated_message' => __('Formation submited', 'communic'),
              );
    
              acf_form($args);
  • Hello, I love this front end submission form, but the one thing that I am having a hard time with is making the post title = a field from my form. So I have a custom field – “title” and would like to have this as my title of the post. Right now I am just using the default code found here: http://www.advancedcustomfields.com/resources/tutorials/using-acf_form-to-create-a-new-post/

    I would greatly appreciate it if you can help! Thanks!

  • @m3ndi3

    you need to add id of your title field in $post array like this.

            'post_title'  => $_POST['fields']['field_53b5105fc69e0'],  
    

    you can find the field id by checking the element in firebug.

  • FYI, Version 5 has this functionality built in via the acf_form function now. You can create new posts, including title and content, from the front-end without custom functions.

    Check it: http://www.advancedcustomfields.com/resources/functions/acf_form/

  • With acf pro it’s [‘acf’] instead of [‘fields’]

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

You must be logged in to reply to this topic.