Support

Account

Home Forums Add-ons Repeater Field Repeater field saved in content

Solved

Repeater field saved in content

  • Hello i want to create a frontend form and to use a repeater field that has 2 fields an image field and textarea.
    I want to save the repeater field insite the post content and i don’t know how to achieve that.
    i know how i can do it with a text field or textarea
    $postContent = $_POST[‘acf’][‘field_xxxxxx’]
    $post = array(
    ‘post_type’ => ‘post’,
    ‘post_status’ => ‘pending’,
    ‘post_title’ => wp_strip_all_tags($_POST[‘acf’][‘field_xxxxx’]),
    ‘post_content’ => $postContent,
    );

  • a repeater field is a nested array in $_POST[‘acf’]

    
    $_POST['acf'][$repeater_field_key][$ROW_INDEX][$subfield_key]
    

    So you need to loop through the repeater field to get each row and then get the sub field

    
    foreach ($acf[$repeater_field_key] as $row) {
      $image_id = $row[$sub_field_key];
    }
    

    ACF stores attachment IDs for images, so if you want to insert it into the content you’ll need to use wp function to get the attachment information and build the image tag. https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_src/

  • This is my code

    
    add_filter('acf/pre_save_post' , 'rtm_pre_save_post' );
    function rtm__save_post( $post_id ) {
      $category = $_POST['acf']['field_54dfccb977a11']; 
      $pasiPregatire = $_POST['acf']['field_58197932d0cff']; // << i know this is an array and won't work
       $postContent = $_POST['acf']['field_54dfc94e35ec5'] /* << text area field */ .   $pasiPregatire	;
    // check if this is to be a new post
    if( $post_id != 'new' ) {
       return $post_id;
    }
    // Create a new post
    	$post = array(
    		'post_type'     => 'post', 
    		'post_status'   => 'pending',
    		'post_title'    => wp_strip_all_tags($_POST['acf']['field_54dfc93e35ec4']),
    		'post_content'  => $postContent,
    		'post_category'  => array($category),
    	);
    	// insert the post
    	$post_id = wp_insert_post( $post );
    	// Save the fields to the post
    	do_action( 'acf/save_post' , $post_id );
    	return $post_id;
    }

    But i don’t realize how to put $pasiPregatire (even with the foreach loop) in $postContent

  • if this is your repeater field $pasiPregatire = $_POST['acf']

    
    foreach ($pasiPregatireas $row) {
      $image_id = inval($row[$image_sub_field_key];
      $size = 'full'; // or the size you want to get
      $image = wp_get_attachment_image($image_id, $size);
      $postContent .= $image;
    }
    
  • Thanks for your hints.
    Did some digging and tweaking, and this code works

    
    $pasiPregatire = $_POST['acf']['field_58197932d0cff'];
    foreach ($pasiPregatire as $row) 
        {
            $image_id = $row['field_58197943d0d00'];
            $size = 'full'; // or the size you want to get
            $imageRet = wp_get_attachment_image($image_id, $size);
            $textRet = $row['field_58197952d0d01'];
            $retetaPas .= '<div>' . $imageRet . $textRet . '</div>';
        }
    $postContent = $_POST['acf']['field_54dfc94e35ec5'] . $retetaPas;
    

    I will work on something for nested repeaters.

  • Sorry about the typo in my code, glad you got it working.

    Nested repeaters need nested loops

    Let’s say that the key for the repeater is field_123 and the key for the nested repeater is key_321

    
    if (!empty($_POST['acf']['key_123']) && is_array($_POST['acf']['key_123'])) {
      foreach ($_POST['acf']['key_123'] as $row) {
        // nested repeater
        if (!empty($row['key_321']) && is_array($row['key_321'])) {
          foreach ($row['key_321'] as $sub_row) {
            $something = $sub_row['some-field-key'];
          }
        }
      }
    }
    
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Repeater field saved in content’ is closed to new replies.