Support

Account

Home Forums Front-end Issues acf_form() always creates two posts

Solved

acf_form() always creates two posts

  • Hi

    I have tried using

    acf_form(array(
    		'post_id'		=> 'new_post',
    		'post_title'	=> true,
    		'new_post'		=> array(
    			'post_type'		=> 'candidates',
    			'post_status'	=> 'publish'
    		)
    	));

    with no pre-save filter and also simply

    acf_form(array(
    		'post_id'		=> 'new',
    		'field_groups' => array(7,18),
    		'submit_value'		=> 'Book'
    	)); 

    with a pre-save filter like

     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 candidate',
             'post_type'  => 'candidates',
         );  
    
         // insert the post
         $post_id = wp_insert_post( $post ); 
    
         // return the new ID
         return $post_id;
     }
    
     add_filter('acf/pre_save_post' , 'my_pre_save_post' );

    and both ways I always get two new posts created. I really can’t figure out what’s going on.

    Any ideas?

    Thank you!

    James

  • If you use 'post_id' => 'new_post', ACF will automatically create a new post using the values form the new_post array. ACF should not be creating a new post if you use just new for the post_id.

    Do you have other front end forms that are creating new posts? If you do you’ll need to use a different post_id for each. For example in your above code I would use something like `new_candidates_post’

    
    acf_form(array(
      'post_id' => 'new_candidates_post',
      'field_groups' => array(7,18),
      'submit_value' => 'Book'
    ));
    
    
    function my_pre_save_post ($post_id) {
      // check if this is to be a new post for candidates
      if ($post_id != 'new_candidates_post') {
        return $post_id;
      }
      // Create a new post ....
     }
    
  • Hi John

    Thanks for your reply. It’s not that neither way doesn’t work, because both the methods I mentioned and yours all work, it’s that it always creates TWO posts when I submit the form.

    I really can’t work it out. Could it be a WordPress setting because I guess no-one else is having this problem…?

    Thanks again,

    James

  • There are only a couple of reason that I can think of for duplicate posts being created. One is that there is another pre_save_posts filter running. The second is that there is another plugin that’s creating a new post.

    Have you tried disabling all other plugins on the site? Do you know if there are any other filters running when the form is submitted?

    One last thing that may cause it, but it’s way out there, is calling acf_form_head() twice.

  • Thanks again John. The only plugins activated are ACF Pro and the ACF repeater field. I haven’t added any other pre_save_post anywhere else (not in my header, page, footer or functions.php) and I only have one acf_form_head() which is at the top of my single-post.php file, just before get_header().

    I guess if no one else is having this problem it must just be something to do with my set up?

    Thanks again for all your help…

  • You have ACF Pro and ACF repeater field installed? You shouldn’t need ACF repeater if you have ACF Pro. Disable the repeater plugin.

  • With just ACF Pro installed on a default theme.

    I added this to 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 candidate',
             'post_type'  => 'candidates',
         );  
    
         // insert the post
         $post_id = wp_insert_post($post); 
    
         // return the new ID
         return $post_id;
     }
    
     add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    

    Then I put acf_form_head(); in my template file before get_header(); and I added this to the content are of the template file

    
    
    $args = array(
    	'post_id'		=> 'new',
    	'submit_value'		=> 'Book',
    	'field_groups' => array(3304)
    );
    acf_form($args);
    

    It uses a different field group but other than that everything is the same. I even set up the custom post type you’re using. Everything works as expected.

    To figure out where the problem is you need to start removing things. Make sure only ACF Pro is installed and switch to on of the default WP 20XX themes. It could also be something in the theme you’re using.

  • Hi John

    I can’t thank you enough for your help. I think it must be my theme because I carried out your instructions in the 2015 theme and it worked fine. I am using wordpress naked which I assumed wouldn’t be the problem because it has nothing in it but clearly there is something there which is wrecking it.

    Thanks again – much appreciated!

    James

  • Is this the theme you are using? http://naked-wordpress.bckmn.com/

    I’m curious now as to what’s causing it.

  • It’s this one but I take all the functions out of the functions.php file. Looking at it now, it is quite an old theme…

    https://code.google.com/p/wordpress-naked/

  • I just took a look at it and I don’t see anything in it that would cause a problem, so that’s very odd.

  • I’m having the same problem. The front end form works perfectly fine, but no matter what I do it creates two posts. I have no other plugins installed and my theme is just an Underscores.me theme that I’ve added very basic functionality too.

  • I never worked out what it was about my theme which caused the problem, but changing the theme did fix it for me. Which I know isn’t very helpful but might be a good place to start…

  • Thanks! I actually did sorta resolve it. I was trying to use a page template specifically for this front end form. Literally the only version in which the form works is when I place the acf_form_head function above my template name code. Not above doctype in my header, not via an ‘init’ hook, this was the only way it worked for me.

    <?php acf_form_head();?>
    <?php
    //Template Name: New Project
    ?>
  • I wanted to share my recent experience with this very same problem. No matter what I tried, any front-end form I created with acf_form was creating duplicates. I’ve worked with ACF Pro for a long time and had never run into this problem on other projects.

    Based on the advice here, I tried switching themes, and the problem stopped. So I knew something was up with the custom theme I’d built. I finally found the problem by creating a blank new theme, then rebuilding the theme file-by-file to find the exact template file that caused the error.

    FINALLY I discovered that the problem started when I added footer.php. After investigating that file, I noticed that ‘<?php wp_footer(); ?>’ was missing before the closing body tag. Adding that immediately fixed the problem.

    Missing the call to wp_footer() seems to have been the culprit in my case. Hopefully this helps someone else out there.

  • I’m sure that will help others @scotty-v, it’s something that I didn’t think about before but will keep in mind if others have this problem in the future. As a theme developer wp_footer() is one of those things that I take for granted that it’s there because it’s one of those things that can cause all kinds of strange issues when it’s not. In this case it’s probably because the scripts needed by ACF are included in the footer on the front end of the site and not having them somehow causes posts to be duplicated, not that I understand why that would happen even knowing it does. 😛

  • @scotty-v I would love to rank your answer to the top because this totally was the right call.

  • Hi a quick solution for this issue is to prevent post to save if the title(or anything you want) already exists, paste this code in your function.php
    it’s also recommended to add more conditions to your if statement like post type as this function might affect other plugins

    /*Check if post is duplicated*/
    function disable_save( $maybe_empty, $postarr ) {
    	if ( ! function_exists( 'post_exists' )) {
        require_once( ABSPATH . 'wp-admin/includes/post.php' );
    	}
        if(post_exists($postarr['post_title']) && $postarr['post_type'] == '/*your post type name*/' )
        {
            /*This if statment important to allow update and trash of the post and only prevent new posts with new ids*/
            if(!get_post($postarr['ID']))
        	{
        	      $maybe_empty = true;
            }
        }
        else
        {
        	$maybe_empty = false;
        }
    
        return $maybe_empty;
    }
    add_filter( 'wp_insert_post_empty_content', 'disable_save', 999999, 2 );
  • Great Scotty V!!!!!!!!!!!!!!

  • This is how I fixed the problem:
    I used the init hook to load in acf_form_head() like so:

    function form_head() {
      if (!is_admin()) {
        acf_form_head();
      }
    }
    
    add_action('init', 'form_head');

    At first I used the init hook without the is_admin check, this caused that acf_form_head to load twice/post twice.

    The solution is to use the init hook with is_admin() to prevent it loading twice.

  • Thanks for helping pinpoint this, we were facing this exact issue but are developing within a plugin rather than a template which prevents us from putting the function at the top of the template file we wanted.

    What we found, rather than adding to init is to inject into wp_head with a priority of 1 to force it to appear first. Figured I’d add here as well just to give some alternative options.

    add_action( 'wp_head', 'inject_acf_form_head', 1 );
    function inject_acf_form_head( ) {
        acf_form_head();
    }
  • I’m having the same problem, but this solution is showing a Cannot modify header information warning after the form is submitted.

  • @ismael87
    I was tried your solution, It fixed my issue. But I have new problem. I can’t add new post in admin dashboard. Are you have a idea???

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

The topic ‘acf_form() always creates two posts’ is closed to new replies.