Support

Account

Home Forums Front-end Issues Using acf_form for multiple post_type Reply To: Using acf_form for multiple post_type

  • Hi Enrico,

    I think I see what you mean 🙂 So you’re creating a repeated field which has these fields (just an example):

    1. Title
    2. Post Type
    3. Post Content

    When user’s save the field you would like to create posts based on the information the user has entered.

    If this is correct you could use the acf/save_post action. This runs once the values for the repeater are saved. You can then retrieve them using the usual method, you could do something like this:

    add_action( 'acf/save_post', 'my_custom_creator' );
    function my_custom_creator( $post_id ) {
        if( have_rows('your_repeater') ):
            while ( have_rows('your_repeater') ) : the_row();
            
            $postdata = array(
                'post_title' => get_sub_field( 'title' ),
                'post_content' => get_sub_field( 'content' ),
                'post_type' => get_sub_field( 'post_type' );
            );
    
            $new_post_id = wp_insert_post( $postdata );
     
            endwhile;
        }
    }
    

    Do be careful though as this will create a new post each time you save the fields. You can either delete the post where the options are right after creating the new post, or you can save the ID’s of the newly created post and make sure they are not created, only updated if the options are saved again.

    Hope I could help,

    Daniel