Skip Navigation

[Resolved] Creating and linking child posts to parent post

This support ticket is created 5 years, 8 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 5 replies, has 2 voices.

Last updated by Christian Cox 5 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1230462

Tell us what you are trying to do?
I have a form that creates products and this is working ok.
I have also set-up the edit form for this.
Products have a one to many relationships to experiences (one product can have many experiences).
Experiences have custom fields attached to them.
I now want to create a form that allows users to create multiple experiences and add them to a product.
I am trying to follow the documentation.

I have created the relationship.
I have created a relationship form.

I can't seem to get the result though of creating experiences for a product.

The overall flow I'm looking for is as follows:
1. Users registers on site (already done)
2. User creates a product (form)
3. User then is directed to another page where they then view product entry and add experiences for that product.
4. Either:
a. On current page from step 3, they then can see the overall result and if needed edit either product or any of listing
b. Or go to another page to view all entries and then select any experiences for any edits.

Is there any documentation that you are following?
https://toolset.com/documentation/post-relationships/how-to-build-front-end-forms-for-connecting-posts/

Is there a similar example that we can see?

What is the link to your site?
hidden link

#1230691

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.

#1230814

Hi, that worked great. The only problem then is on the child forms having a counting system so that the form is hidden after a certain number of entries. In other words, the user is into a continual loop on a page entering child posts.

#1230984

You can wrap the Form shortcode in a conditional that tests the number of child posts for the current parent post. Here is a shortcode that returns the number of child posts:

function ts_count_children_func( $atts )
{
  $atts = shortcode_atts( array(
    'parent' => get_the_ID(),
    'rel' => ''
  ), $atts );
  $children = toolset_get_related_posts(
    $atts['parent'],
    $atts['rel'],
    'parent',
    100000, 0,
    array(),
    'post_id',
    'child'
  );
  return count( $children );
}
add_shortcode( 'ts-count-children', 'ts_count_children_func' );

Use it like this in the parent post template:

[ts-count-children rel="your-relationship-slug"]

Replace your-relationship-slug.

#1231744

HI Christian, that is extremely useful.
I currently have the child form on a different page and pass the url of the parent to the form.
If I use this shortcode on the child form page and wrap it with a conditional will that enable me to conditionally display the form?

Gary

#1231868

The shortcode accepts a parent attribute, and you can add any arbitrary parent ID there.

[ts-count-children rel="your-relationship-slug" parent="12345"]

However, converting the parent URL to the parent ID with a shortcode might exceed the number of nested shortcode attributes, depending on how your conditional is set up. If that's the case, you need more custom code to get the parent ID from the URL directly without a shortcode attribute.