Skip Navigation

[Resolved] Saving repeating field group with generic fields

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to save their RFG using php.

Solution:

The solution can be seen in the link below.
https://toolset.com/forums/topic/saving-repeating-field-group-with-generic-fields/#post-1202453

This support ticket is created 5 years, 9 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 4 replies, has 2 voices.

Last updated by Timothy 5 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#1200797

Hi,

I'm trying to save a repeating field group using generic fields in a User Form to a cpt.

I have a repeating field group called Coach Education with has 3 fields (Coach School Name, Coach Degree, Coach Year Graduated). And I have a User Form in the link below that creates a new user and a Coach cpt. The form also uses generic fields to create populate the Coach cpt custom fields.

hidden link

I've tried to use Generic fields to populate this repeating field group but it is not working. Is this a limitation in toolset?

In my form I have these 3 generic fields:

     <label>School Name</label>
    
      [cred_generic_field type='textfield' field='coach-school-name']
      {
      "required":0,
      "validate_format":0,
      "default":""
      }
      [/cred_generic_field]
  </div>
  
    <div class="col-sm-5 formspacer ">
    <label>Degree</label>
      [cred_generic_field type='textfield' field='coach-school-degree']
      {
      "required":0,
      "validate_format":0,
      "default":""
      }
      [/cred_generic_field]
  </div>
  
   <div class="col-sm-2 formspacer ">
    <label>Year Graduated</label>
      [cred_generic_field type='numeric' field='coach-year-graduated']
      {
      "required":0,
      "validate_format":0,
      "default":""
      }
      [/cred_generic_field]
  </div>

And this is what I have in functions.php:

add_action('cred_save_data', 'save_coach_cpt_init',10,2);

function save_coach_cpt_init($post_id, $form_data) {

	if ($form_data['id']==127)
    {
	    $post_id = wp_insert_post( array(
	                    'post_status' => 'publish',
	                    'post_type' => 'coach',
			               'post_author' => $post_id,
	                    'post_title' => $_POST['first_name'].' '.$_POST['last_name'],
	                    'post_content' => ''
	                ) );
	    update_post_meta($post_id, 'wpcf-coach-first-name', $_POST['first_name']);
		update_post_meta($post_id, 'wpcf-coach-last-name', $_POST['last_name']);
		update_post_meta($post_id, 'wpcf-coach-email', $_POST['user_email']);
		update_post_meta($post_id, 'wpcf-coach-street-address', $_POST['coach-street-address']);
		update_post_meta($post_id, 'wpcf-coach-phone', $_POST['coach-phone']);
		update_post_meta($post_id, 'wpcf-coach-website', $_POST['coach-website']);
		update_post_meta($post_id, 'wpcf-coach-zip-code', $_POST['coach-zip-code']);
		update_post_meta($post_id, 'wpcf-coach-description', $_POST['coach-description']);
		update_post_meta($post_id, 'wpcf-coach-gender', $_POST['coach-gender']);
		update_post_meta($post_id, 'wpcf-coach-video', $_POST['coach-video']);
		update_post_meta($post_id, 'wpcf-coach-specialties', $_POST['coach-specialties']);
		update_post_meta($post_id, 'wpcf-years-practicing', $_POST['years-practicing']);
		update_post_meta($post_id, 'wpcf-coach-photo', $_POST['coach-photo']);
		update_post_meta($post_id, 'wpcf-coaching-certificate-image', $_POST['coaching-certificate-image']);
		update_post_meta($post_id, 'wpcf-coach-certificate', $_POST['coach-certificate']);
		update_post_meta($post_id, 'wpcf-training-qualifications-experience', $_POST['training-qualifications-experience']);
		update_post_meta($post_id, 'wpcf-coach-street-address', $_POST['coach-street-address']);
		update_post_meta($post_id, 'wpcf-coach-state', $_POST['coach-state']);
		update_post_meta($post_id, 'wpcf-coach-city', $_POST['coach-city']);
		update_post_meta($post_id, 'wpcf-coach-linkedin-profile', $_POST['coach-linkedin-profile']);
		update_post_meta($post_id, 'wpcf-coach-education', $_POST['coach-education']);
		update_post_meta($post_id, 'wpcf-coach-fee', $_POST['coach-fee']);
		
		update_post_meta($post_id, 'wpcf-coach-school-name', $_POST['coach-school-name']);
		update_post_meta($post_id, 'wpcf-coach-school-degree', $_POST['coach-school-degree']);
		update_post_meta($post_id, 'wpcf-coach-year-graduated', $_POST['coach-year-graduated']);
		
		wp_set_post_terms( $post_id, $_POST['coach-specialties'], 'coach-specialty');

	}
}

Thanks,

Tim

#1201282

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

Thanks for getting in touch. The repeatable field groups are actually liked like in my example below.
https://toolset.com/forums/topic/saving-repeatable-field-group/#post-1188112

Thanks,
Shane

#1201370

Thanks, I think I'm close, this is what I have now, but it's giving a Fatal Error when I submit the form:

"Fatal error: Uncaught InvalidArgumentException: The parent must be a post ID or a WP_Post instance."

add_action('cred_save_data', 'save_coach_rfg',10,2);
 function save_coach_rfg($post_id, $form_data) {
	if ($form_data['id']==127)    {

//Create a new Repeating Field Group Item
   $new_rfg = array(
     'post_title'    => 'New Education Item',
     'post_status'   => 'publish',
     'post_type' => 'coach-education'
   );

  $new_rfg_id = wp_insert_post( $new_rfg );
  //Add Fields to the RFG
   update_post_meta($new_rfg_id , 'wpcf-coach-school-name', $_POST['coach-school-name']);
   update_post_meta($new_rfg_id , 'wpcf-coach-degree', $_POST['coach-degree']);
   update_post_meta($new_rfg_id , 'wpcf-coach-year-graduated', $_POST['coach-year-graduated']);

  //Now connect the new RFG to a Post
    toolset_connect_posts('coach-education', $coach_post->ID, $post_id);

	}
}
#1202453

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

The issue is actually here


    toolset_connect_posts('coach-education', $coach_post->ID, $post_id);

Not sure where you are getting the $coach_post->ID from since there isn't any reference to it.

I also suggest you come the code to this function that you've already created.

The reason being that you will need the ID of the post that was created as well as the id of the RFG. So the idea would be like this.


function save_coach_cpt_init($post_id, $form_data) {
 
    if ($form_data['id']==127)
    {
        $post_id = wp_insert_post( array(
                        'post_status' => 'publish',
                        'post_type' => 'coach',
                        'post_title' => $_POST['first_name'],
                        'post_content' => ''
                    ) );
        update_post_meta($post_id, 'wpcf-coach-first-name', $_POST['first_name']);
        update_post_meta($post_id, 'wpcf-coach-last-name', $_POST['last_name']);
        update_post_meta($post_id, 'wpcf-coach-email', $_POST['user_email']);
        update_post_meta($post_id, 'wpcf-coach-street-address', $_POST['coach-street-address']);
        update_post_meta($post_id, 'wpcf-coach-phone', $_POST['coach-phone']);
        update_post_meta($post_id, 'wpcf-coach-website', $_POST['coach-website']);
        update_post_meta($post_id, 'wpcf-coach-zip-code', $_POST['coach-zip-code']);
        update_post_meta($post_id, 'wpcf-coach-description', $_POST['coach-description']);
        update_post_meta($post_id, 'wpcf-coach-gender', $_POST['coach-gender']);
        update_post_meta($post_id, 'wpcf-coach-video', $_POST['coach-video']);
        update_post_meta($post_id, 'wpcf-coach-specialties', $_POST['coach-specialties']);
        update_post_meta($post_id, 'wpcf-years-practicing', $_POST['years-practicing']);
        update_post_meta($post_id, 'wpcf-coach-photo', $_POST['coach-photo']);
        update_post_meta($post_id, 'wpcf-coaching-certificate-image', $_POST['coaching-certificate-image']);
        update_post_meta($post_id, 'wpcf-coach-certificate', $_POST['coach-certificate']);
        update_post_meta($post_id, 'wpcf-training-qualifications-experience', $_POST['training-qualifications-experience']);
        update_post_meta($post_id, 'wpcf-coach-street-address', $_POST['coach-street-address']);
        update_post_meta($post_id, 'wpcf-coach-state', $_POST['coach-state']);
        update_post_meta($post_id, 'wpcf-coach-city', $_POST['coach-city']);
        update_post_meta($post_id, 'wpcf-coach-linkedin-profile', $_POST['coach-linkedin-profile']);
        update_post_meta($post_id, 'wpcf-coach-education', $_POST['coach-education']);
        update_post_meta($post_id, 'wpcf-coach-fee', $_POST['coach-fee']);

//Create a new Repeating Field Group Item
   $new_rfg = array(
     'post_title'    => 'New Education Item',
     'post_status'   => 'publish',
     'post_type' => 'coach-education'
   );
 
  $new_rfg_id = wp_insert_post( $new_rfg );
  //Add Fields to the RFG
   update_post_meta($new_rfg_id , 'wpcf-coach-school-name', $_POST['coach-school-name']);
   update_post_meta($new_rfg_id , 'wpcf-coach-degree', $_POST['coach-degree']);
   update_post_meta($new_rfg_id , 'wpcf-coach-year-graduated', $_POST['coach-year-graduated']);
 
  //Now connect the new RFG to a Post
    toolset_connect_posts('coach-education', $post_id, $new_rfg_id);
 
    }
}

Please try this and let me know.

Thanks,
Shane

#1202759

That works perfectly.

Thanks,

Tim