Support

Account

Home Forums General Issues acf_form – Create one or multiple Posts at once Reply To: acf_form – Create one or multiple Posts at once

  • This solution worked for me… with help from here: update repeater field on acf/save_post

    Thanks.

    // This function imports multiple posts at once
    function acf_import_multiple_posts( $post_id ) {
      // If the post_id is not equal to 'new', skip the entire function
      if( $post_id != 'new' ) {
        return $post_id;
      };
    
      $loop = $_POST['acf']['field_5718505fe3643']; //ACF Repeater Field key
      $customMetaField = 'customMetaField'; // Custom Meta Field
      $customPostTitle = 'customPostTitle'; // Custom Title
    
      $count = count($loop); // Get the number of rows in the Repeater Field
    
      for ($key = 0; $key < $count; ++$key) { // If $count is 5, run for loop till it reaches 5 times
        // Swap out $key for each row number
        $customMetaField = trim($_POST['acf']['field_5718505fe3643'][$key]['field_57185028c6ae9']);
        $customPostTitle = trim($_POST['acf']['field_5718505fe3643'][$key]['field_57185028c6b00']);
    
        // Create a new App Post from posts listed in the repeater field
        $post = array(
          'post_status'  => 'draft', // Set to draft because the user still needs to review
          'post_title' => $customPostTitle, // Actual Post Title
          'post_name' => sanitize_title( $customPostTitle ), // Actual Post Slug
          'post_type'  => ‘post’ // Post Type
        );
    
        $post_id = wp_insert_post( $post );     // Use the WordPress default wp_insert_post function
    
        // Set the Custom Meta Fields with the $customMetaField and $customPostTitle
        add_post_meta($post_id, 'customMetaField', $customMetaField, true);
        add_post_meta($post_id, 'customPostTitle', $customPostTitle, true);
      }
    
      // update $_POST['return']
      $_POST['return'] = add_query_arg( array('post_id' => $post_id), $GLOBALS['acf_form']['return'] );
    
      // return the new ID
      return $post_id;
    }
    add_action('acf/pre_save_post', 'acf_import_multiple_posts', 20);