• Resolved ds7ilg7

    (@ds7ilg7)


    Hello Konrad,

    Background: You assisted me with a similar question before, although I wasn’t using a repeater.

    I have a front end form that when submitted, creates a parent custom post with a form Action. Within the form, I am using a repeater to collect data in which the entries will be used to create child posts.

    With your plugin, I can easily create the parent post. With additional code, I can loop the repeater to successfully create multiple child posts that are assigned correctly to the parent, but I’m having trouble updating the sub fields for the child posts.

    The form Action is a standard ‘Create Post’, and I ‘Save’ all of the fields in the form. This creates the parent post.

    The code for creating the child posts is below:

    `// Post New Contractor and Create Multiple PM posts from repeater
    function post_new_pm ($post_id, $type, $args, $form, $action){

    // get the repeater
    $values = get_field(‘pm_1’, $post_id);

    // loop through the repeater
    foreach( $values as $value ) {

    // set the child project manager post arguments
    $new_post = array(
    ‘post_title’ => $value[‘pm1_first’],
    ‘post_type’ => ‘contractor’,
    ‘post_parent’ => $post_id,
    ‘post_status’ => ‘draft’,
    );
    // post pm
    $new_post_id = wp_insert_post($new_post);

    update_sub_field(‘field_6025b4cfd7121’, $value[‘pm1_first’], $new_post_id);
    update_sub_field(‘field_6025b4cfd7122’, $value[‘pm1_last’], $new_post_id);
    update_sub_field(‘field_6025b4cfd7123’, $value[‘pm1_phone’], $new_post_id);
    update_sub_field(‘field_6025b4cfd7124’, $value[‘pm1_email’], $new_post_id);
    }
    }
    add_action(‘acfe/form/submit/post/form=test-post-form’, ‘post_new_pm’, 10, 5);

    I appreciate your help. Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback! You’re in the good hook to perform the action you’re trying to achieve. However your code to update the repeater looks off. Please take a look at the documentation to perform an update on a Repeater:

    You’ll also find an example in this ACF support topic. I would recommend to first try to update a repeater using PHP out of the form, so you can debug and test your code more easily. Then once it works, put the code back inside the Form Action.

    Note: You can use the backquote tag to embed code on the WordPress forum 😉

    Hope it helps!

    Have a nice day.

    Regards.

    Thread Starter ds7ilg7

    (@ds7ilg7)

    Thanks Konrad,

    In case anyone has a similar situation, this worked for me:

    // Post New Parent Post  and Create Multiple Child posts from repeater
    function post_project_managers($post_id){
        // get the repeater
        $values = get_field('use_your_repeater_name_here', $post_id);
           
        // loop through the repeater
        foreach( $values as $value ) {
    		
       // set the child post arguments
            $new_post = array(
        			'post_title' => $value[‘name_your_post’], //use any field to name your psot
    			'post_type' => 'post', // set to post or use any CPT
    			'post_parent' => $post_id, //the parent comes from the 1st action of the form		
    			'post_status'	=> 'draft',
    		       );        
    		// post pm
    		$new_post_id = wp_insert_post($new_post);
    
    // Save related repeater field values to the corresponding new post
    $field_key = "field_1234567"; //insert repeater key 
    $value = array(
        array(
            "field_1234568"   => $value['field_name'],
            "field_1234569"   => $value['field_name'],
            "field_1234560"   => $value['field_name'],
            "field_1234570"   => $value['field_name']
    	)
    );
    update_field( $field_key, $value, $new_post_id );
    	}
    }
    add_filter('acf/save_post', 'my_acf_post_field', 20);
    add_filter('acfe/form/submit/post/form=test-post-form', 'post_project_managers', 10, 5);
    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    I’m glad to hear that it’s now working as expected!

    If you enjoy this plugin, feel free to submit a review, it always helps and it’s much appreciated 🙂

    Have a nice day!

    Regards.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Creating Child Posts from a Repeater’ is closed to new replies.