Support

Account

Home Forums ACF PRO Frontend form submitting to custom post type

Solved

Frontend form submitting to custom post type

  • Ok, I’m lost.

    I have a CPT named pov_travel. I have a field group called POV-Travel. My form template is here: http://www.pcscalculator.com/mixed-mode/, the code below is what I’m using in the template.

    <?php
    	
    	acf_form(array(
    		'post_id'		=> 'new_post',
    		'post_title'	=> true,
    		'post_content'	=> true,
    		'new_post'		=> array(
    			'post_type'		=> 'pov_travel',
    			'post_status'	=> 'publish'
    		),
    	  //'return'		=> home_url('contact-form-thank-you'),
    		'submit_value'	=> 'Send'
    	));
    	
    	?>

    In the backend I can see all my fields from the field group on the mixed-mode page. For some reason I cannot get the form to render on the frontend. I grabbed the code from here: https://www.advancedcustomfields.com/resources/using-acf_form-to-create-a-new-post/

    What am I missing?

  • Hi @signalwarrant

    I believe I have replied to your question in the other thread, but I will reply it again here. I think it’s better to keep the discussion over here too.

    Could you please tell me the version of ACF you are using? If you are using ACF PRO (version 5), please take a look at this page: https://www.advancedcustomfields.com/resources/using-acf_form-to-create-a-new-post/. If you are using ACF free (version 4), please take a look at this page: https://www.advancedcustomfields.com/resources/using-acf_form-to-create-a-new-post/?version=4.

    Thanks 🙂

  • Here is my template code:

    <?php
    /**
     *
       Template Name: test form Full Width
     *
     * Description: A page template without the left or right columns
     * @package flat_responsive
     * @since 1.0.0
     */
    acf_form_head(); 
    get_header(); ?>
    
    	<div id="primary">
    		<div id="content" role="main">
    
    			<?php /* The loop */ ?>
    			<?php while ( have_posts() ) : the_post(); ?>
    				
    		  <h1><?php //the_title(); ?></h1>
    				<?php
    				if(isset($_GET['updated'])){
    					if($_GET['updated'] == 'true'){
    						echo '<p style="color:green;font-weight:bold">The file details have been submitted.</p>';
    					} 
    				} 
    				?>
    		  		<?php 
    				  acf_form(array(
    					  'id' => 'POV-Travel',
    					  'post_id'		=> 'new_post',
    					  'field_groups'	=> array(431),
    					  'submit_value'		=> 'Submit',
    				));
    				?>
    				
    		  
    
    			<?php endwhile; ?>
    
    		</div><!-- #content -->
    	</div><!-- #primary -->
    
    <?php get_footer(); ?>

    Here is my functions code (I’m assuming we’re about the functions.php in my theme):

    ///////////////////////////////////////////////
    // Form Processor
    ///////////////////////////////////////////////
    
    function pre_save_post( $post_id ) {
    	
    	// stop function if not a new post
    	if( $post_id !== 'new_post' ) {
    		return $post_id;
    	}
    	
    	// vars
    	$title = $_POST['fields']['field_577eeca76c4be'];
    	$dla = $_POST['fields']['field_577d8805fbf95'];
    	$miles = $_POST['fields']['field_577d9ddb09284'];
      	$service_member = $_POST['fields']['field_577d9e4e61f79'];
    	$deps_over_12 = $_POST['fields']['field_577da50b58664'];
      	$deps_under_12 = $_POST['fields']['field_577da57183151'];
    	$malt = $_POST['fields']['field_577da5d6049df'];
      
    	// Create a new post
    	$post = array(
    		'post_status'	=> 'publish',
    		'post_type'		=> 'pov_travel',
    		'post_title'	=> $title,
    	);	
    	
    
    	// insert the post
    	$post_id = wp_insert_post( $post ); 
    	
    	// return the new ID
    	return $post_id;
    	
    }

    The form renders on the frontend OK here: http://www.pcscalculator.com/mixed-mode/

    When I try to submit, the frontend appears to work but nothing is submitted to the custom post type.

    I can submit to this CPT from the backend with no problems. I have taken code snippets from https://www.advancedcustomfields.com/resources/using-acf_form-to-create-a-new-post/ and Leanne’s form https://support.advancedcustomfields.com/forums/topic/displaying-form-on-front-end-location-rules-issues/.

    My field group is set to only appear on post type pov travel.

    I’m running ACF 5 Pro.

  • Mission complete, had to add.

    'new_post'		=> array(
    'post_type'		=> 'pov_travel',
    'post_status'	=> 'publish'),
  • This reply has been marked as private.
  • This reply has been marked as private.
  • Hi!
    By following this thread I was able to do the same, thank you very much!
    Now I’m facing the problem of getting the user login from the from submission, in order to use it as the author of my CPT.
    Using codes below I’m able to retrieve the user login ID in order to create a post title as “user_ID – Title”, but the post author still remains empty.
    Can you help me please?
    Thank you very much!

    Here my codes.

    Form template:

    $new_post = array(
    		'post_id'            => 'new_post',
    		'field_groups'       => array(46),
    		'form'               => true,
    		'html_before_fields' => '',
    		'html_after_fields'  => '',
    		'submit_value'       => 'Submit Post',
    		'updated_message'    => 'Saved!'
    	);
    	acf_form( $new_post );

    Functions.php:

    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    
    function my_pre_save_post( $post_id ) {
    	
    	// bail early if not a new post
    	if( $post_id !== 'new_post' ) {
    		 
    		return $post_id;
    		
    	}
    	
    	
    	// vars
    	$title = $_POST['fields']['field_59c6d5f47898c'];
    	global $current_user;
    		  get_currentuserinfo();
        
    	
    	// Create a new post
    	$post = array(
    		'post_status'	=> 'publish',
    		'post_type'		=> 'ws_trip',
    		'post_title'	=> $current_user->user_login.'-'.$title,
            'post_author'	=> $current_user->user_login,
         
    
    	);	
    	
    	
    	// insert the post
    	$post_id = wp_insert_post( $post ); 
    
    	
    	// return the new ID
    	return $post_id;
  • Solved!

    'post_author' => $current_user->ID,

    is the right way!

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

You must be logged in to reply to this topic.