Support

Account

Forum Replies Created

  • 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);
  • Hi @elliot

    Is it ok to have multiple acf_form on one page that saves / update to the same post? Reasonable use for this is to have different acf field_groups in accordion style.

    Thanks.

  • This is the code that works for me. Note the $GLOBALS['acf_form']['return']

    function my_pre_save_post( $post_id )
    {
        // check if this is to be a new post
        if( $post_id != 'new' ) {
            return $post_id;
        };
    
        $title = trim($_POST['acf']['field_5702e3df3ce82']);
    
        // Create a new post
        $post = array(
            'post_status'  => 'publish' ,
            'import_id' => $_POST['acf']['field_5702b6a15e788'],
          	'post_title' => $title,
          	'post_name' => sanitize_title( $title ),
            'post_type'  => 'app'
        );
    
        // insert the post
        $post_id = wp_insert_post( $post );
    
        // 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_filter('acf/pre_save_post' , 'my_pre_save_post');
  • Thanks,

    That is what I had expected.

  • What I am actually doing is just loading default subfields (with no values) into a repeater. Just empty subfields the user can field out.

    So basically, I am pre-adding subfields to a repeater, so I do not have to add the fields manually.

    The subfields are created automatically using acf/load_field/type=flexible_content.

    function acf_flexible_repeater_subfields( $field ) {
      $field['sub_fields'] = array(
        array(
          'key' => 'headline',
          'label' => 'Headline',
          'name' => 'headline',
          'type' => 'text'
        )
      )
    }
    add_filter('acf/load_field/type=repeater', 'acf_flexible_repeater_subfields');

    Now, this works perfectly. Keep in mind – doing it this way does not allow you to add child fields via ACF. You will have to add it within the same acf/load_field/type=repeater hook.

    I believe you want to add default values – which you can do like this:

    function acf_flexible_repeater_subfields( $field ) {
      $field['sub_fields'] = array(
        array(
          'key' => 'headline',
          'label' => 'Headline',
          'name' => 'headline',
          'type' => 'text',
          'default_value' => 'Hi' // ADD THIS LINE
        )
      )
    }
    add_filter('acf/load_field/type=repeater', 'acf_flexible_repeater_subfields');

    Test it out in functions.php if that is what you are trying to achieve.

  • Yes, I saw that post also ;). So the code I added in the other thread does work, it just adds multiple empty fields when you add another field to that Field Group. I just cannot figure out why. It only happens to the sub_fields $field though.

  • @John – Thanks for the reply. I was able to kind of find a solution for this in this thread here: load_field – repeater adds empty fields to Field Group, however there is an issue with ACF adding multiple empty fields on Field Group update when having an array inside of 'sub_fields' => array().

    It would be best if I close this and continue on the other thread in hand.

    Thanks.

    But to answer you question – I am trying to create default subfields without having to add them manually via ACF.

Viewing 7 posts - 1 through 7 (of 7 total)