Hi, if it's a one-to-many relationship then you probably don't need a separate relationship Form. You need two new post Forms, one to create the parent posts and another to create child posts. You probably need two edit post Forms as well, one to edit parent posts and another to edit child posts. Here's how I would set it up.
- Create a Form that creates new parent posts. In the Form settings, choose to "display the post" after the Form is submitted.
- Place the Form that creates parent posts on a custom Page.
- Create a Form that edits parent posts. Place this Form in an unassigned Content Template.
- Create another Content Template, this one for single parent posts. Add a Forms "Edit Post" link and choose the edit post Content Template you just created. Now you have a link from the parent post to a Form where the post can be edited.
- Create a Form that creates new child posts. In the Form editor, set the parent post input default value using the post ID of the current page: [wpv-post-id item="$current_page"]. This will predefine the parent post and link the new child to the proper parent. You can also hide the input field with CSS if you'd like.
- Set the Form that creates new child posts to redirect to some Page. It doesn't matter which page you choose, we will use a custom code snippet to simply reload the parent post. This will reset the Form for another new child post entry, and update the View of child posts to include the new child post (a few notes below). Replace 12345 with the numeric ID of the new child posts Form.
add_filter('cred_success_redirect', 'tssupp_reload_page', 10, 3);
function tssupp_reload_page($url, $post_id, $form_data) {
if (in_array($form_data['id'], array(12345))) { //Edit the form ID
if (isset($_REQUEST['_cred_cred_prefix_cred_container_id'])) {
$url = get_permalink($_REQUEST['_cred_cred_prefix_cred_container_id']);
}
}
return $url;
}
- Add the Form that creates child posts to the parent post Content Template. Now you can create child posts directly from the parent post.
- Create a Form that edits child posts and set it to redirect to any Page. Use another snippet to redirect to the parent post (not the child post). Replace 12345 with the numeric ID of the edit child posts Form, and replace 'your-child-post-type-slug' with your child post type's slug.
add_filter('cred_success_redirect', 'custom_redirect_child_post_editor',10,3);
function custom_redirect_child_post_editor($url, $post_id, $form_data)
{
$forms = array( 12345 );
$child_slug = 'your-child-post-type-slug';
if ( in_array( $form_data['id'], $forms ) ) {
$parent_id = toolset_get_related_post( $post_id, $child_slug );
return get_permalink($parent_id);
}
return $url;
}
- Place the Form that edits child posts in an unassigned Content Template.
- Create a View of child posts, add a post relationship Query Filter, where the parent post is set by the page where the View is shown. In the loop of this View, display information about each child post and a Forms "Edit post" link (choose the Content Template from previous step). insert this View in the parent post Content Template so you can see all the child posts related to the current parent post, with a link to edit each one.