Support

Account

Home Forums Backend Issues (wp-admin) Create Multiple Posts on Back End Reply To: Create Multiple Posts on Back End

  • Hi @karks88

    I’ve created another way to do it by using “acf/save_post” instead of “acf/pre_save_post” for you. Here’s the front end form:

    acf_form(array(
        'post_id' => 'new_post',
        'new_post' => array(
            'post_type'	=> 'post',
            'post_status'	=> 'publish'
        ),
    ));

    And here’s the functions.php:

    function my_acf_save_multiple_posts( $post_id ) {
        
        // get the repeater
        $values = get_field('new_posts', $post_id);
        
        // loop through the repeater
        foreach( $values as $value ) {
            
            // set the new post arguments
            $new_post = array(
                'post_title' => $value['post_title'],
                'post_content' => $value['post_content'],
                'post_type' => 'cpt',
            );
            
            // post it!
            $new_post_id = wp_insert_post($new_post);
            
            // add the custom fields if you have any (you need to use the field key!!!)
            update_field('field_1234567890abc', $value['post_meta_custom_fields'], $new_post_id);
            
        }
        
        // delete the unused post
        wp_delete_post($post_id);
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_multiple_posts', 20);

    I’ve also attached the JSON export file (if you use the PRO version). If you use the free version, please examine the JSON file to see the options I’ve created instead.

    I hope this helps 🙂