Support

Account

Home Forums General Issues Create post from acf_form in other post type and set custom fields

Solved

Create post from acf_form in other post type and set custom fields

  • Hi,

    I have an acf_form located on a specific page.
    When saving the form, a custom post is created :

    $args = array(
    	'post_id' => 'new_post', // On va créer une nouvelle publication
    	'new_post' => array(
    		'post_type' => 'enquiry',
    		'post_status' => 'publish',
    		),
    	'field_groups' => array(3496), 
    );	
    acf_form($args);

    This works fine, because that specific post type has the same fields as the form. However, how do I create a post with a separate CPT and set some of its custom fields with values contained in the submitted form?

    I tried to go another way by removing ‘new_post’ altogether from $args, and using wp_insert_post in a separate function, but I didn’t succeed.

  • OK, I managed to do it with a acf/save_post function.

    add_filter('acf/save_post', 'create_proposal_from_form');  
    
    function create_proposal_from_form($post_id) {
    	
    	$test = get_field('test',$post_id);
    	$pagetype = is_page_template();
    	
    	if ($pagetype = 'Proposal Form') {
    		
    		$post_data = array(
    				'post_title' => 'Test7', 
    				'post_status' => 'publish',
    				'post_name' => 'test1',
    				'post_type' => 'enquiry',
    			);
    
    		$newpageid = wp_insert_post( $post_data );	
    		update_field('form_event_nights',$test, $newpageid);	
    	}
    }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.