Skip Navigation

[Resolved] Add repeatedly relationship custom post at frontend

This support ticket is created 3 years 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)

Author
Posts
#1979681

Dear Sir/Madam,

I have CPT tours, applications, applicants, a customer will register a tour and fill in the enrolment form (application) with a group of applicants (applicants), CPT applicant contains the custom fields like name, gender, age, citizen id, the birth of date, tel, etc. One customer can apply for many applications, an application must link with one tour, an application also contain many applicants.

Please advise how I can build a frontend form to allow a customer to register the tour and add the applicants as many as he wants.

I know how to enable the field as the repeatable field in custom fields, CPT applicant is not a single field but it is a group of fields and it is a repeatable record under an application.

I look forward to hearing from your suggestion.

Best regards,

Kelvin.

#1979853

Hello, you can use Toolset Forms to create one post or to create one repeatable field group (RFG) entry in a post, but it is not currently possible to create/manage RFGs in the same Form that creates the parent post. The recommended procedure in this case is to use one Toolset Form to create the main Application post, then redirect to that Application post. Display another Form to add one Applicant RFG to that main Application post, and a View of Applicants for the current Application. If you want to create Applicant RFGs in the same Form that creates the parent Application post, custom programming is required. You can use wp_insert_post to create an Applicant RFG, then use the post relationships API toolset_connect_posts to connect the Applicant RFG to the Application post.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

Let me know if you need additional information about that.

#1980327
Screenshot 2021-03-10 at 6.50.51 AM.png
Screenshot 2021-03-10 at 6.45.09 AM.png

Dear Christian Cox,

Thanks for your advise, I can accept to have another form after I create an application, so I have to create a form for the application, after submit the application form, how can I be redirected to another form applicant and the newly created application automatically be the parent of this applicant form and I can have a button to add new applicant as a repeated record in the applicant form?

Refer to the screenshot, this is the application, I don't need the button to connect any existing applicant, I want to do the same adding new applicant but do it in frontend and also want to create applicant in tabular look ( see screenshot sample)

I do create a CRED but no idea how I can reapply it

[credform]
	[cred_field field='form_messages' class='alert alert-warning']
<div class="row">
	<div class="col">[cred_field field='applicant-full-name' force_type='field' class='form-control' output='bootstrap' placeholder="Name"]</div>
	<div class="col">[cred_field field='applicant-gender' force_type='field' class='form-control' output='bootstrap']</div>
	<div class="col">[cred_field field='applicant-bod' force_type='field' class='form-control' output='bootstrap']</div>
	<div class="col">[cred_field field='applicant-type' force_type='field' class='form-control' output='bootstrap']</div>
	<div class="col">[cred_field field='applicant-hkid' force_type='field' class='form-control' output='bootstrap' placeholder="HKID"]</div>
	<div class="col">[cred_field field='applicant-passport-number' force_type='field' class='form-control' output='bootstrap' placeholder="Passport Number"]</div>
	<div class="col">[cred_field field='@application-applicants.parent' class='form-control' output='bootstrap' select_text='--- not set ---' required='false']</div>
	<div class="col">[cred_field field='form_submit' output='bootstrap' value='Submit' class='btn btn-primary btn-lg']</div>
</div>
[/credform]

Could you please give me some instruction?

Best regards,

Kelvin.

#1981111

after submit the application form, how can I be redirected to another form applicant and the newly created application automatically be the parent of this applicant form
In the Application Form, choose "After visitors submit this form: Display the post" to redirect to the new Application post. In the Application post Content Template, display a View of existing Applicants (post relationship / repeatable field group owner filter set by "The post where this View is shown"). The View will be empty at first, but after adding Applicants the View will begin to show a list of all Applicants connected to the Application post.

Also in the Application post Content Template, display the Form to create new Applicant RFGs. To set the parent Application automatically, use the value attribute in the post relationship field, set to the current post ID using a wpv-post-id shortcode:

[cred_field field='@application-applicants.parent' class='form-control' output='bootstrap' select_text='--- not set ---' required='false' value='[wpv-post-id]']

The parent Application will be selected automatically when the page loads. Hide the field with CSS if you do not want to allow Users to change the default parent:

<div style="display:none;">[cred_field field='@application-applicants.parent' class='form-control' output='bootstrap' select_text='--- not set ---' required='false' value='[wpv-post-id]']</div>

In the new Applicant Form settings, choose "Keep displaying this Form" and disable AJAX. When the Form is submitted to create a new Applicant RFG, the page will reload, the View of Applicants will update, and the Form will be displayed again so the User can immediately create another Applicant.

I do create a CRED but no idea how I can reapply it
I believe you are asking how to create multiple Applicant RFG rows in a single CRED Form, but this is not possible. A CRED Form can only add or edit one RFG row or post. It is not possible to combine multiple form rows in one submission, or create multiple rows of RFG in a single CRED Form submission. You would have to create your own form system entirely using custom code. When the form is submitted you would have to manually insert each row of the RFG as a new post using wp_insert_post. The post_type slug for an RFG is the same as the RFG slug. After inserting a row, connect the new Applicant RFG row to its parent Application post with the post relationships API toolset_connect_posts. The post relationship slug is the same as the RFG slug, the parent ID is the parent Application ID, and child ID is the new Applicant RFG post ID returned by wp_insert_post.

#1981833

Dear Christian Cox,

If I have my custom form, so I need to have the application post saved and get the post id, then I can do wp_insert_post to update the applicant. Right?

Best regards,

Kelvin.

#1982619

If I have my custom form, so I need to have the application post saved and get the post id, then I can do wp_insert_post to update the applicant. Right?
You can use wp_insert_post to create each Applicant post, then use toolset_connect_posts to connect the Applicant and the Application. Let us assume your New Application Form has the ID 123. This example cred_save_data hook shows how you can programmatically create one new Applicant RFG row and connect it to the parent Application post:

// Example to automatically create RFG and assign it to parent post when submitting new parent post Form
// https://toolset.com/forums/topic/add-repeatedly-relationship-custom-post-at-frontend/
add_action('cred_save_data', 'tssupp_save_application_action',10,2);
function tssupp_save_application_action($post_id, $form_data) {
  $forms = array( 123 ); 
  $applicant_rfg_slug = 'applicant-rfg';

  if ( in_array( $form_data['id'], $forms ) )
  {
    // Create applicant post object
    $applicant_args = array(
      'post_title'    => 'Test post',
      'post_content'  => 'Lorem ipsum',
      'post_status'   => 'publish',
      'post_type'     => $applicant_rfg_slug,
      'post_author'   => 1,
      'meta_input'    => array(
        'wpcf-applicant-field-1' => 'Field 1 value',
        'wpcf-applicant-field-2' => 'Field 2 value'
      )
    );
    // insert applicant post and get new Applicant post ID
    $applicant_id = wp_insert_post( $applicant_args );

    // connect applicant RFG to parent application post ($post_id is application post)
    toolset_connect_posts( $applicant_rfg_slug, $post_id, $applicant_id );
  }
}

Just an example to show you how it could work.

#1985759
Screenshot 2021-03-14 at 11.58.21 PM.png

Dear Christian Cox,

I will have the application CRED form including the applicant HTML form inside, is it possible to pass the new application post id back to the CRED form and then I can insert this id as the parent application post id to the applicant post? I know how to get the new saved post in from cred_save_data() but how can I pass it back to my applicant HTML form if I keep displaying this form

The application CRED form will be like this

[credform]
		[cred_field field='application-tour-number' force_type='field' class='form-control' output='bootstrap' urlparam='tour-id']
		[cred_field field='form_messages' class='alert alert-warning']
		...
		[cred_field field='form_submit' output='bootstrap' value='Submit' id='application-submit' class='btn btn-primary btn-lg'][/credform]

<form id="application-details" class="" method="post" action="">
   	<input type="hidden" name="applicant-tour-number" value="application_post_id">
   	...
        <input class="btn btn-success" type="submit" value="Submit"></center>
</form>

You should find the application_post_id in my applicant HTML form, when I submit the form, I can call ajax to do the wp_insert_post and toolset_connect_posts

I capture the CRED form design for your reference. If it can't, how can pass the new saved application post id to another new CRED form?

#1985783

Sorry duplicated reply. Please read previous one #1985759

#1985899

I think you have a few options:
1. Let us assume you place the New Application Form on a page https://yoursite.com/page-with-new-application-form/. Use the redirection API cred_success_redirect to redirect to the same page with the new Application ID added to a URL parameter. https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect

add_filter('cred_success_redirect', 'tssupp_application_id_redirect',10,3);
function tssupp_application_id_redirect($url, $post_id, $form_data)
{
    if ($form_data['id']==6789)
        return '<em><u>hidden link</u></em>' . $post_id;
   
    return $url;
}

2. In a cred_save_data or cred_submit_complete hook for the New Application Form, save the new Application ID in a PHP session variable. Call toolset_connect_posts when your custom Applicant form is submitted, and get the value of the Application ID from the PHP session variable.
https://www.php.net/manual/en/session.examples.basic.php

#1986325

Dear Christian Cox,

Sound good. Excellent. If I use API cred_success_redirect to same the application CRED form and show the applicant HTML form, will the customer confuse to fill another application or CRED form will have special handling?

Best regards,

Kelvin.

If I

#1987081

This workflow sounds confusing. I think a different workflow would be better, what do you think?
- Create a Page containing the New Application Form. Do not include the New Applicant Form on this page, only New Application Form.
- In Application Form settings, choose the option "Display the post" after Form is submitted.
- User submits New Application Form and is redirected to the new Application post they just created.
- In the Application post Content Template, include a link back to the Create Application page, "Create another Application". This makes it easy to immediately create another Application if necessary, or continue in this page to create new Applicants.
- Also in the Application post Content Template, include a View of Applicants related to the current Application post. This makes it easy to see the existing Applicants for this Application. The View will be empty at first, but as the User creates Applicants the list will grow.
- Also in the Application post Content Template, include the New Applicant Form. Set the parent Application to be the same as the current page. This makes it easy to begin creating related Applicants immediately after creating the Application.
- Use the redirection API to reload the current Application post after the Applicant Form is submitted.
- After the User submits New Applicant Form, the page reloads. The View of Applicants is updated to show all related Applicants including the new Applicant, and the Form to create New Applicants is displayed again so the User can create more Applicants. Also the link to Create another Application is still displayed, so the User can easily jump back to the New Application page.

#1991413

Dear Christian Cox,

Many thanks for your detailed explanation. I got your suggestion but only not clear to the Applicant Form in the Application Post as part of the content.

As you know the applicant form is a dynamic form that I can instantly create the applicant record before I submit the form, I will have my custom script to save the multiple applicant record using wp_insert_post and toolset_connect_posts, and then redirect to the application post again.

In the applicant CRED form, I will make like this

[credform]
[cred_generic_field type='hidden' field='tour-id']
{
"default":""
}
[/cred_generic_field]

	<input type="hidden" name="action" value="register">
  	<div class="">
		<div class="row">
			<div class="col">
				<table style="max-width: 100%; " id="myTable" class="table order-list table-hover table-sm table-responsive ">
					<tbody>
 						<tr id="start">
 							<td>
 							{ applicant fields .... }
 							</td>
							<td><button id="addrow" type="button" class="btn btn-success">+</button></td>
						</tr>
					</tbody>
				</table>
              <center><input class="btn btn-success" type="submit" value="Submit"></center>
		</div>
	</div>

[/credform]

Question 1
I know how to assign the field value using urlparam if the post having the field

[cred_field field='application-tour-number' force_type='field' class='form-control ' output='bootstrap' urlparam='tour-id']

There is a relationship between Application and Applicant, so I don't need to have the field tour-id to the Applicant, I have no idea how I can pass the urlparam "tour-id" and put it as the default value of the cred_generic_field "tour-id", this value is important when I redirect to next page.

[cred_generic_field type='hidden' field='tour-id']
{
"default":""
}
[/cred_generic_field]

Question 2
I assume [credform].... [/credform] will automatically generate the < from > ... < /form >, I don't need to declare it but how I can do the form submission? How can I get the form id and I can trigger the elements inside the form?

#1992203

I have no idea how I can pass the urlparam "tour-id" and put it as the default value of the cred_generic_field "tour-id", this value is important when I redirect to next page.
If the Form is displayed at a URL like https://yoursite.com/page-with-form/?param=1234, then you can use the wpv-search-term shortcode to access the param URL parameter and use it in a generic hidden field:

[cred_generic_field type='hidden' field='tour-id']
{
"default":"[wpv-search-term param='param']"
}
[/cred_generic_field]

Search term shortcode documentation: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-search-term

I assume [credform].... [/credform] will automatically generate the < from > ... < /form >, I don't need to declare it but how I can do the form submission?
I'm not sure I understand. There is no JavaScript API or AJAX API for Toolset Forms, and a Toolset Form can only edit or create one post at a time. If you want to create a form that allows multiple post submissions you must create it in HTML and JavaScript without using Toolset Forms. Maybe it would be helpful if I can log in and see how you have things set up so far? Then I can understand better what you want to accomplish and make good recommendations for you.

#1992529

I will insert my custom form between [credform]...[/credform], there is no submit button to my custom form, I want to call the cred_save_data when user submit the form, how can I add the [credform] submit button.
The credform tags will generate an HTML form when rendered in the page. You cannot add form tags inside credform shortcodes, because that would produce nested form tags. Nested form tags are invalid in HTML.

I have no idea how I can pass the urlparam "tour-id" and put it as the default value of the cred_generic_field "tour-id", this value is important when I redirect to next page.
I'm trying to understand the workflow, and how Tour information is passed around to the different Forms. Let's say I start at this Tour:
hidden link
How do I get to the Application Form to create an Application for this specific Tour? Do you plan to show the New Application Form in this Content Template for Tours?
hidden link
Or do you have a View of Tours somewhere else with a link to the Application Form for each Tour?

#1992915
Screenshot 2021-03-19 at 8.48.55 AM.png
Screenshot 2021-03-19 at 8.48.37 AM.png
Screenshot 2021-03-19 at 8.48.21 AM.png

Dear Christian Cox,

I fixed the issue.

Answer your question, I will have a tour directory list, and each contains the new application link with the tour id, after the customer submits the application, an application (custom content template) show up with the applicant form, in the applicant form, it contains the newly added application post id and the tour id, when applicant form submitted, all applicants' record will be saved and oolset_connect_posts() will be executed.

Now I face the date field update issue via the wp_insert_post() to toolset custom field

						$applicant_args = array(
							'post_title'    => '....',
							'post_status'   => 'publish',
							'post_type'     => 'applicant',
							'meta_input'    => array(
							....
							  'wpcf-applicant-bod' => $_POST["bod_{$i}"],
							)
						);
					}
					$applicant_post_id = wp_insert_post( $applicant_args );

Refer to the frontend input, the applicant record from the dashboard, and the relationship with the application, how can I insert the CPT applicant with the correct date field?

For frontend, I use

<input type="date" class="form-control" name="bod_0">

to record the date value.

For the applicant from the dashboard, all date fields are recorded as of today date

For the application relationship, all date fields are recorded as the first day of a date value.

Please advise how I can correctly save the date field to toolset custom date field?

Best regards,

Kelvin

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.