Support

Account

Home Forums Front-end Issues ACF form in front end based in ACF field Reply To: ACF form in front end based in ACF field

  • Oh dear, I’m a dumb!

    My problem was that the reference ID was wrong, so would never work. But not to be so ugly, I share my finding.

    The code to create a form based on another is:

    Front End

    <?php 	
    $customtype = get_field('page_customtype'); 		
    $idform = get_field('page_idform');
       $args = array(
               'post_id'		=> 'newpost_'.$customtype ,
               'field_groups'	=> array( $idform ),
               'return' => add_query_arg( 'updated', 'true',   "#" ),
       );
    acf_form( $args ); 
    ?>
    

    And the part that makes me proud is how to treat the data to insert without having to have the reference (KeyField) of each field:

    functions.php

    function my_pre_save_post( $post_id ) {
    	
    		$customtype = explode('_' , $post_id );
    
    		$post = array(
    					'post_status'	=> 'Publish',
    					'post_type'		=> $customtype[1],
    				);	
    
    		$post_id = wp_insert_post( $post ); 
    
    		while (list($key, $value) = each($_POST['acf'])) {
    
    			$var = $var . "Key: $key; Value: $value<br />\n";
    			$namefield =  get_field_object($key);
    				
    			add_post_meta ($post_id, $namefield['name'] , $value, true);
    		}
    
    	}
    	add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
    

    Thus a job that took hours (depending on the number of forms) only takes a few minutes.

    I hope I can help in any way