Support

Account

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

Solved

acf_form – Create one or multiple Posts at once

  • Hello.

    If we have a acf_form set up on a custom page (Add Post) to create a new post – is it possible to create one or multiple posts at one time?

    ie.

    <?php acf_form(array(
      'post_id' => 'new',
      'form' => false
    ));?>

    Allows you to create one post, but if I wanted to create 10 posts from a single acf_form newpost form, is it possible?

  • Hi @spotlight100

    I believe it’s possible, but you need an advanced knowledge of PHP to do it. Instead of multiplying acf_form(), I think you can use a repeater field so you can add and remove the number of posts you want.

    After that, you need to use the acf/save_post hook and loop through the posted repeater data using the $_POST[‘acf’] variable then save it using the wp_insert_post() function.

    I hope this makes sense. Thanks!

  • 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);
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘acf_form – Create one or multiple Posts at once’ is closed to new replies.