Creating Child Posts from a Repeater
-
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.
The topic ‘Creating Child Posts from a Repeater’ is closed to new replies.