Skip Navigation

[Resolved] Access repeating field and insert it into a new posts

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a repeating custom field in a custom post type. When submitting a Form that creates this custom post type, I would like to create a cloned post that copies the values from the repeating custom field and inserts them automatically in the post clone.

Solution: Use wp_insert_post in a cred_save_data hook to create a clone of the original post. Get the repeating field value from the $_POST superglobal, and loop over those values in a foreach loop, or other similar loop structure. Use add_post_meta to insert each value into the custom field in the cloned post.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
  $forms = array( 123 );
  if ( in_array( $form_data['id'], $forms ) )
  {
    $gallery = $_POST['wpcf-fieldslug'];
    $new_book_args = array(
      'post_title' => 'cloned book test from ' . $post_id,
      'post_status' => 'publish',
      'post_type' => 'book'
    );
 
    // Insert the cloned post into the database
    $new_book_id = wp_insert_post( $new_book_args );
     
    foreach( $gallery as $img ){
      add_post_meta($new_book_id, 'wpcf-fieldslug', $img, false);
    }
  }
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/wp_insert_post/
https://developer.wordpress.org/reference/functions/add_post_meta/

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by Lara 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1922019

Tell us what you are trying to do?

I have a follow up question to my last question (https://toolset.com/forums/topic/access-custom-checkboxes-field-_postwpcf-slug-as-an-array/#post-1919451)

After submitting a CRED Form, I try to clone the submitted post, modify it slightly and insert it into the database. Works very good so far, but I encountered a problem with a repeating picture field...

'wpcf-repeating-picture-field' =>$_POST['wpcf-repeating-picture-field'], (see code below) ... doesn't work

// Create post object
$termin = array(
'post_title' => $day,
'post_status' => 'publish',
'post_type' => 'event',              
  
'meta_input'  => array ( 
'wpcf-repeating-picture-field' =>$_POST['wpcf-repeating-picture-field'],      
),
    
);


// Insert the post into the database
$termin_id = wp_insert_post( $termin );

I also tried ...

// add pictures   
$saved_pictures = get_post_meta($post_id,'wpcf-repeating-picture-field',true);    
foreach($saved_pictures as $pictures):
add_post_meta($termin_id, 'wpcf-repeating-picture-field', $pictures, false);         
endforeach;

... but it only adds an empty element to the new post.

I'm looking for a way to access a repeating field and then insert it into a new posts

Is there any documentation that you are following?
no

Is there a similar example that we can see?
no

What is the link to your site?
Site under development

#1922121
Screen Shot 2021-01-28 at 11.00.26 AM.png

Hi, the solution depends on the options in your Post Form configurations, as seen in the screenshot here.
1. Is the Form set to submit using AJAX?
2. Is the Form set to use the Media Library for images?

Please share your configurations and I can offer some additional guidance.

#1922139
2021-01-28__Repeating Field.PNG

Hi Christian,

many thanks for your answer.
I attached an image with my Post Form configuration.

Kind regards
Lara

#1922175

Okay I see, thank you. The following example shows how you can access custom field images as an array in the $_POST superglobal in a cred_save_data callback, then loop over the images in that array and insert them in a new post using wp_insert_post and add_post_meta:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
  $forms = array( 123 );
  if ( in_array( $form_data['id'], $forms ) )
  {
    $gallery = $_POST['wpcf-fieldslug'];
    $new_book_args = array(
      'post_title' => 'cloned book test from ' . $post_id,
      'post_status' => 'publish',
      'post_type' => 'book'
    );

    // Insert the cloned post into the database
    $new_book_id = wp_insert_post( $new_book_args );
    
    foreach( $gallery as $img ){
      add_post_meta($new_book_id, 'wpcf-fieldslug', $img, false);
    }
  }
}

It's similar to some of the code you shared, but instead of using get_post_meta I am accessing the images directly in the $_POST superglobal.

#1923045

Many thanks Christian.

It works perfectly.

I have a similar problem with another repeating field (date). I need to iterate over it and access the information, that is saved inside the['datepicker']. It would be lovely, if you could give me a hint. I hoped that it would work similar to the picture repeating field, but it doesn't....

// random days   
$random_days = $_POST['wpcf-rask-weitere-tage']['datepicker']; 
foreach($random_days as $k=>$v) {

$day = $v['datepicker'];

}
#1926791

Instead of looping over the datepicker array value, loop over the field itself. Here is a small modification that adjust your foreach loop source:

$random_days = $_POST['wpcf-rask-weitere-tage']; 
foreach($random_days as $k=>$v) {
 
  $day = $v['datepicker'];
  add_post_meta(12345, 'wpcf-rask-weitere-tage', $day, false);
}

Change 12345 to a variable containing the new (clone) post ID returned by wp_insert_post.

#1926879

Thanks Christian 🙂 You're the man!
Works perfectly